nushell/crates/nu-command/src/commands/dataframe/series/all_false.rs
Fernando Herrera 17008bb648
Removed list from dataframe command signatures (#3713)
* Type in command description

* filter name change

* Clean column name

* Clippy error and updated polars version

* Lint correction in file

* CSV Infer schema optional

* Correct float operations

* changes in series castings to allow other types

* Clippy error correction

* Removed lists from command signatures

* Added not command for series
2021-07-01 16:33:52 +12:00

68 lines
1.9 KiB
Rust

use crate::{commands::dataframe::utils::parse_polars_error, prelude::*};
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{dataframe::NuSeries, Signature, TaggedDictBuilder, UntaggedValue, Value};
pub struct DataFrame;
impl WholeStreamCommand for DataFrame {
fn name(&self) -> &str {
"dataframe all-false"
}
fn usage(&self) -> &str {
"[Series] Returns true if all values are false"
}
fn signature(&self) -> Signature {
Signature::build("dataframe all-false")
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
command(args)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Returns true if all values are false",
example: "[$false $false $false] | dataframe to-series | dataframe all-false",
result: None,
},
Example {
description: "Checks the result from a comparison",
example: r#"let s = ([5 6 2 8] | dataframe to-series);
let res = ($s > 9);
$res | dataframe all-false"#,
result: None,
},
]
}
}
fn command(mut args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let series = NuSeries::try_from_stream(&mut args.input, &tag.span)?;
let bool = series.as_ref().bool().map_err(|e| {
parse_polars_error::<&str>(
&e,
&tag.span,
Some("all-false only works with series of type bool"),
)
})?;
let res = bool.all_false();
let value = Value {
value: UntaggedValue::Primitive(res.into()),
tag: tag.clone(),
};
let mut data = TaggedDictBuilder::new(tag);
data.insert_value("all_false", value);
Ok(OutputStream::one(data.into_value()))
}