diff --git a/crates/nu-command/src/commands/dataframe/mod.rs b/crates/nu-command/src/commands/dataframe/mod.rs index 85100460e..b0fc5eadb 100644 --- a/crates/nu-command/src/commands/dataframe/mod.rs +++ b/crates/nu-command/src/commands/dataframe/mod.rs @@ -79,6 +79,7 @@ pub use series::DataFrameIsUnique; pub use series::DataFrameNNull; pub use series::DataFrameNUnique; pub use series::DataFrameNot; +pub use series::DataFrameReplace; pub use series::DataFrameSeriesRename; pub use series::DataFrameSet; pub use series::DataFrameSetWithIdx; diff --git a/crates/nu-command/src/commands/dataframe/series/mod.rs b/crates/nu-command/src/commands/dataframe/series/mod.rs index d7387be78..7e4309721 100644 --- a/crates/nu-command/src/commands/dataframe/series/mod.rs +++ b/crates/nu-command/src/commands/dataframe/series/mod.rs @@ -14,6 +14,7 @@ pub mod n_null; pub mod n_unique; pub mod not; pub mod rename; +pub mod replace; pub mod set; pub mod set_with_idx; pub mod shift; @@ -36,6 +37,7 @@ pub use n_null::DataFrame as DataFrameNNull; pub use n_unique::DataFrame as DataFrameNUnique; pub use not::DataFrame as DataFrameNot; pub use rename::DataFrame as DataFrameSeriesRename; +pub use replace::DataFrame as DataFrameReplace; pub use set::DataFrame as DataFrameSet; pub use set_with_idx::DataFrame as DataFrameSetWithIdx; pub use shift::DataFrame as DataFrameShift; diff --git a/crates/nu-command/src/commands/dataframe/series/replace.rs b/crates/nu-command/src/commands/dataframe/series/replace.rs new file mode 100644 index 000000000..b59d957fb --- /dev/null +++ b/crates/nu-command/src/commands/dataframe/series/replace.rs @@ -0,0 +1,72 @@ +use crate::{commands::dataframe::utils::parse_polars_error, prelude::*}; +use nu_engine::WholeStreamCommand; +use nu_errors::ShellError; +use nu_protocol::{dataframe::NuSeries, Signature, SyntaxShape}; +use nu_source::Tagged; +use polars::prelude::IntoSeries; + +pub struct DataFrame; + +impl WholeStreamCommand for DataFrame { + fn name(&self) -> &str { + "dataframe replace" + } + + fn usage(&self) -> &str { + "[Series] Replace the leftmost (sub)string by a regex pattern" + } + + fn signature(&self) -> Signature { + Signature::build("dataframe replace") + .required_named( + "pattern", + SyntaxShape::String, + "Regex pattern to be matched", + Some('p'), + ) + .required_named( + "replace", + SyntaxShape::String, + "replacing string", + Some('r'), + ) + } + + fn run(&self, args: CommandArgs) -> Result { + command(args) + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Replaces string", + example: "[abc abc abc] | dataframe to-series | dataframe replace -p ab -r AB", + result: None, + }] + } +} + +fn command(mut args: CommandArgs) -> Result { + let tag = args.call_info.name_tag.clone(); + let pattern: Tagged = args.req_named("pattern")?; + let replace: Tagged = args.req_named("replace")?; + + let series = NuSeries::try_from_stream(&mut args.input, &tag.span)?; + + let chunked = series.as_ref().utf8().map_err(|e| { + parse_polars_error::<&str>( + &e, + &tag.span, + Some("The replace command can only be used with string columns"), + ) + })?; + + let res = chunked + .as_ref() + .replace(pattern.as_str(), replace.as_str()) + .map_err(|e| parse_polars_error::<&str>(&e, &tag.span, None))?; + + Ok(OutputStream::one(NuSeries::series_to_value( + res.into_series(), + tag, + ))) +} diff --git a/crates/nu-command/src/commands/mod.rs b/crates/nu-command/src/commands/mod.rs index f738d5f11..329c3dc50 100644 --- a/crates/nu-command/src/commands/mod.rs +++ b/crates/nu-command/src/commands/mod.rs @@ -31,9 +31,9 @@ pub use dataframe::{ DataFrameFilter, DataFrameFirst, DataFrameGet, DataFrameGroupBy, DataFrameIsDuplicated, DataFrameIsIn, DataFrameIsNotNull, DataFrameIsNull, DataFrameIsUnique, DataFrameJoin, DataFrameLast, DataFrameList, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFrameNot, - DataFrameOpen, DataFramePivot, DataFrameSample, DataFrameSelect, DataFrameSeriesRename, - DataFrameSet, DataFrameSetWithIdx, DataFrameShape, DataFrameShift, DataFrameShow, - DataFrameSlice, DataFrameSort, DataFrameTake, DataFrameToCsv, DataFrameToDF, + DataFrameOpen, DataFramePivot, DataFrameReplace, DataFrameSample, DataFrameSelect, + DataFrameSeriesRename, DataFrameSet, DataFrameSetWithIdx, DataFrameShape, DataFrameShift, + DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameTake, DataFrameToCsv, DataFrameToDF, DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts, DataFrameWhere, DataFrameWithColumn, }; diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 2f82656d6..7458faefd 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -318,6 +318,7 @@ pub fn create_default_context(interactive: bool) -> Result