From b10325dff1d78fc7971a93ebe0a7b8fc391966c2 Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Tue, 4 Jun 2024 18:51:11 -0700 Subject: [PATCH] Allow int values to be converted into floats. (#13025) Addresses the bug found by @maxim-uvarov when trying to coerce an int Value to a polars float: image Conversion now works correctly: Screenshot 2024-05-31 at 14 28 51 --- .../values/nu_dataframe/conversion.rs | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs index f7941bf41d..fe0a956129 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_dataframe/conversion.rs @@ -269,13 +269,36 @@ fn typed_column_to_series(name: &str, column: TypedColumn) -> Result, _> = column .values .iter() - .map(|v| v.as_f64().map(|v| v as f32)) + .map(|v| match v { + Value::Float { val, .. } => Ok(*val as f32), + Value::Int { val, .. } => Ok(*val as f32), + x => Err(ShellError::GenericError { + error: "Error converting to f32".into(), + msg: "".into(), + span: None, + help: Some(format!("Unexpected type: {x:?}")), + inner: vec![], + }), + }) .collect(); Ok(Series::new(name, series_values?)) } DataType::Float64 => { - let series_values: Result, _> = - column.values.iter().map(|v| v.as_f64()).collect(); + let series_values: Result, _> = column + .values + .iter() + .map(|v| match v { + Value::Float { val, .. } => Ok(*val), + Value::Int { val, .. } => Ok(*val as f64), + x => Err(ShellError::GenericError { + error: "Error converting to f64".into(), + msg: "".into(), + span: None, + help: Some(format!("Unexpected type: {x:?}")), + inner: vec![], + }), + }) + .collect(); Ok(Series::new(name, series_values?)) } DataType::UInt8 => {