-
Notifications
You must be signed in to change notification settings - Fork 2k
Add DataFrame fill_null #14769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DataFrame fill_null #14769
Changes from 5 commits
3a7efc0
c334f5d
1575a1f
7a1b99c
d517fba
884a2ff
b9bbbd3
b28756f
36fd408
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -51,13 +51,17 @@ use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; | |||||
| use datafusion_common::config::{CsvOptions, JsonOptions}; | ||||||
| use datafusion_common::{ | ||||||
| exec_err, not_impl_err, plan_err, Column, DFSchema, DataFusionError, ParamValues, | ||||||
| SchemaError, UnnestOptions, | ||||||
| ScalarValue, SchemaError, UnnestOptions, | ||||||
| }; | ||||||
| use datafusion_expr::dml::InsertOp; | ||||||
| use datafusion_expr::{case, is_null, lit, SortExpr}; | ||||||
| use datafusion_expr::{ | ||||||
| utils::COUNT_STAR_EXPANSION, TableProviderFilterPushDown, UNNAMED_TABLE, | ||||||
| case, | ||||||
| dml::InsertOp, | ||||||
| expr::{Alias, ScalarFunction}, | ||||||
| is_null, lit, | ||||||
| utils::COUNT_STAR_EXPANSION, | ||||||
| SortExpr, TableProviderFilterPushDown, UNNAMED_TABLE, | ||||||
| }; | ||||||
| use datafusion_functions::core::coalesce; | ||||||
| use datafusion_functions_aggregate::expr_fn::{ | ||||||
| avg, count, max, median, min, stddev, sum, | ||||||
| }; | ||||||
|
|
@@ -1926,6 +1930,71 @@ impl DataFrame { | |||||
| plan, | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| /// Fill null values in specified columns with a given value | ||||||
| /// If no columns are specified, applies to all columns | ||||||
| /// Only fills if the value can be cast to the column's type | ||||||
| /// | ||||||
| /// # Arguments | ||||||
| /// * `value` - Value to fill nulls with | ||||||
| /// * `columns` - Optional list of column names to fill. If None, fills all columns | ||||||
| pub fn fill_null( | ||||||
| &self, | ||||||
| value: ScalarValue, | ||||||
| columns: Option<Vec<String>>, | ||||||
| ) -> Result<DataFrame> { | ||||||
| let cols = match columns { | ||||||
| Some(names) => self.find_columns(&names)?, | ||||||
| None => self | ||||||
|
||||||
| .logical_plan() | ||||||
| .schema() | ||||||
| .fields() | ||||||
| .iter() | ||||||
| .map(|f| f.as_ref().clone()) | ||||||
| .collect(), | ||||||
| }; | ||||||
|
|
||||||
| // Create projections for each column | ||||||
| let projections = self | ||||||
| .logical_plan() | ||||||
| .schema() | ||||||
| .fields() | ||||||
| .iter() | ||||||
| .map(|field| { | ||||||
| if cols.contains(field) { | ||||||
| // Try to cast fill value to column type. If the cast fails, fallback to the original column. | ||||||
| match value.clone().cast_to(field.data_type()) { | ||||||
| Ok(fill_value) => Expr::Alias(Alias { | ||||||
| expr: Box::new(Expr::ScalarFunction(ScalarFunction { | ||||||
| func: coalesce(), | ||||||
| args: vec![col(field.name()), lit(fill_value)], | ||||||
| })), | ||||||
| relation: None, | ||||||
| name: field.name().to_string(), | ||||||
| }), | ||||||
| Err(_) => col(field.name()), | ||||||
| } | ||||||
| } else { | ||||||
| col(field.name()) | ||||||
| } | ||||||
| }) | ||||||
| .collect::<Vec<_>>(); | ||||||
|
|
||||||
| self.clone().select(projections) | ||||||
| } | ||||||
|
|
||||||
| // Helper to find columns from names | ||||||
| fn find_columns(&self, names: &[String]) -> Result<Vec<Field>> { | ||||||
| let schema = self.logical_plan().schema(); | ||||||
| names | ||||||
| .iter() | ||||||
| .map(|name| { | ||||||
| schema.field_with_name(None, name).cloned().map_err(|_| { | ||||||
| DataFusionError::Plan(format!("Column '{}' not found", name)) | ||||||
|
||||||
| DataFusionError::Plan(format!("Column '{}' not found", name)) | |
| plan_datafusion_err!("Column '{}' not found", name)) |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we wrap into the Option here? empty vec already serves as None
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Thanks for spotting this.