forked from extern/nushell
Replace command (#3823)
* replace command * cargo fmt * Signature correction
This commit is contained in:
parent
b7215b5dde
commit
9b5db297a6
@ -79,6 +79,7 @@ pub use series::DataFrameIsUnique;
|
|||||||
pub use series::DataFrameNNull;
|
pub use series::DataFrameNNull;
|
||||||
pub use series::DataFrameNUnique;
|
pub use series::DataFrameNUnique;
|
||||||
pub use series::DataFrameNot;
|
pub use series::DataFrameNot;
|
||||||
|
pub use series::DataFrameReplace;
|
||||||
pub use series::DataFrameSeriesRename;
|
pub use series::DataFrameSeriesRename;
|
||||||
pub use series::DataFrameSet;
|
pub use series::DataFrameSet;
|
||||||
pub use series::DataFrameSetWithIdx;
|
pub use series::DataFrameSetWithIdx;
|
||||||
|
@ -14,6 +14,7 @@ pub mod n_null;
|
|||||||
pub mod n_unique;
|
pub mod n_unique;
|
||||||
pub mod not;
|
pub mod not;
|
||||||
pub mod rename;
|
pub mod rename;
|
||||||
|
pub mod replace;
|
||||||
pub mod set;
|
pub mod set;
|
||||||
pub mod set_with_idx;
|
pub mod set_with_idx;
|
||||||
pub mod shift;
|
pub mod shift;
|
||||||
@ -36,6 +37,7 @@ pub use n_null::DataFrame as DataFrameNNull;
|
|||||||
pub use n_unique::DataFrame as DataFrameNUnique;
|
pub use n_unique::DataFrame as DataFrameNUnique;
|
||||||
pub use not::DataFrame as DataFrameNot;
|
pub use not::DataFrame as DataFrameNot;
|
||||||
pub use rename::DataFrame as DataFrameSeriesRename;
|
pub use rename::DataFrame as DataFrameSeriesRename;
|
||||||
|
pub use replace::DataFrame as DataFrameReplace;
|
||||||
pub use set::DataFrame as DataFrameSet;
|
pub use set::DataFrame as DataFrameSet;
|
||||||
pub use set_with_idx::DataFrame as DataFrameSetWithIdx;
|
pub use set_with_idx::DataFrame as DataFrameSetWithIdx;
|
||||||
pub use shift::DataFrame as DataFrameShift;
|
pub use shift::DataFrame as DataFrameShift;
|
||||||
|
72
crates/nu-command/src/commands/dataframe/series/replace.rs
Normal file
72
crates/nu-command/src/commands/dataframe/series/replace.rs
Normal file
@ -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<OutputStream, ShellError> {
|
||||||
|
command(args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
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<OutputStream, ShellError> {
|
||||||
|
let tag = args.call_info.name_tag.clone();
|
||||||
|
let pattern: Tagged<String> = args.req_named("pattern")?;
|
||||||
|
let replace: Tagged<String> = 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,
|
||||||
|
)))
|
||||||
|
}
|
@ -31,9 +31,9 @@ pub use dataframe::{
|
|||||||
DataFrameFilter, DataFrameFirst, DataFrameGet, DataFrameGroupBy, DataFrameIsDuplicated,
|
DataFrameFilter, DataFrameFirst, DataFrameGet, DataFrameGroupBy, DataFrameIsDuplicated,
|
||||||
DataFrameIsIn, DataFrameIsNotNull, DataFrameIsNull, DataFrameIsUnique, DataFrameJoin,
|
DataFrameIsIn, DataFrameIsNotNull, DataFrameIsNull, DataFrameIsUnique, DataFrameJoin,
|
||||||
DataFrameLast, DataFrameList, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFrameNot,
|
DataFrameLast, DataFrameList, DataFrameMelt, DataFrameNNull, DataFrameNUnique, DataFrameNot,
|
||||||
DataFrameOpen, DataFramePivot, DataFrameSample, DataFrameSelect, DataFrameSeriesRename,
|
DataFrameOpen, DataFramePivot, DataFrameReplace, DataFrameSample, DataFrameSelect,
|
||||||
DataFrameSet, DataFrameSetWithIdx, DataFrameShape, DataFrameShift, DataFrameShow,
|
DataFrameSeriesRename, DataFrameSet, DataFrameSetWithIdx, DataFrameShape, DataFrameShift,
|
||||||
DataFrameSlice, DataFrameSort, DataFrameTake, DataFrameToCsv, DataFrameToDF,
|
DataFrameShow, DataFrameSlice, DataFrameSort, DataFrameTake, DataFrameToCsv, DataFrameToDF,
|
||||||
DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts, DataFrameWhere,
|
DataFrameToParquet, DataFrameToSeries, DataFrameUnique, DataFrameValueCounts, DataFrameWhere,
|
||||||
DataFrameWithColumn,
|
DataFrameWithColumn,
|
||||||
};
|
};
|
||||||
|
@ -318,6 +318,7 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
|
|||||||
whole_stream_command(DataFrameTake),
|
whole_stream_command(DataFrameTake),
|
||||||
whole_stream_command(DataFrameSetWithIdx),
|
whole_stream_command(DataFrameSetWithIdx),
|
||||||
whole_stream_command(DataFrameShape),
|
whole_stream_command(DataFrameShape),
|
||||||
|
whole_stream_command(DataFrameReplace),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
#[cfg(feature = "clipboard-cli")]
|
#[cfg(feature = "clipboard-cli")]
|
||||||
|
Loading…
Reference in New Issue
Block a user