mirror of
https://github.com/nushell/nushell.git
synced 2025-04-04 22:48:41 +02:00
# Description Fixes: #12726 and #13185 Previously converting columns that contained null caused polars to force a dtype of object even when using a schema. Now: 1. When using a schema, the type the schema defines for the column will always be used. 2. When a schema is not used, the previous type is used when a value is null. # User-Facing Changes - The type defined by the schema we be respected when passing in a null value `[a]; [null] | polars into-df -s {a: str}` will create a df with an str dtype column with one null value versus a column of type object. - *BREAKING CHANGE* If you define a schema, all columns must be in the schema.
88 lines
2.5 KiB
Rust
88 lines
2.5 KiB
Rust
use crate::dataframe::values::NuExpression;
|
|
use crate::values::{
|
|
cant_convert_err, CustomValueSupport, NuDataFrame, PolarsPluginObject, PolarsPluginType,
|
|
};
|
|
use crate::PolarsPlugin;
|
|
use nu_plugin::{EngineInterface, EvaluatedCall, PluginCommand};
|
|
use nu_protocol::{
|
|
Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, Type,
|
|
};
|
|
use polars::df;
|
|
|
|
pub struct ExprCount;
|
|
|
|
impl PluginCommand for ExprCount {
|
|
type Plugin = PolarsPlugin;
|
|
|
|
fn name(&self) -> &str {
|
|
"polars count"
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Returns the number of non-null values in the column."
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.input_output_types(vec![(
|
|
Type::Custom("expression".into()),
|
|
Type::Custom("expression".into()),
|
|
)])
|
|
.category(Category::Custom("dataframe".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Count the number of non-null values in a column",
|
|
example: r#"[[a]; ["foo"] ["bar"] [null]] | polars into-df
|
|
| polars select (polars col a | polars count)
|
|
| polars collect"#,
|
|
result: Some(
|
|
NuDataFrame::from(
|
|
df!(
|
|
"a" => [2]
|
|
)
|
|
.expect("should not fail"),
|
|
)
|
|
.into_value(Span::unknown()),
|
|
),
|
|
}]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
plugin: &Self::Plugin,
|
|
engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, LabeledError> {
|
|
let value = input.into_value(call.head)?;
|
|
match PolarsPluginObject::try_from_value(plugin, &value)? {
|
|
PolarsPluginObject::NuExpression(expr) => command_expr(plugin, engine, call, expr),
|
|
_ => Err(cant_convert_err(&value, &[PolarsPluginType::NuExpression])),
|
|
}
|
|
.map_err(LabeledError::from)
|
|
}
|
|
}
|
|
|
|
fn command_expr(
|
|
plugin: &PolarsPlugin,
|
|
engine: &EngineInterface,
|
|
call: &EvaluatedCall,
|
|
expr: NuExpression,
|
|
) -> Result<PipelineData, ShellError> {
|
|
NuExpression::from(expr.into_polars().count()).to_pipeline_data(plugin, engine, call.head)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
use crate::test::test_polars_plugin_command;
|
|
use nu_protocol::ShellError;
|
|
|
|
#[test]
|
|
fn test_examples() -> Result<(), ShellError> {
|
|
test_polars_plugin_command(&ExprCount)
|
|
}
|
|
}
|