From bd5de023a1134a4fb43ead5d05084cdc11cc182e Mon Sep 17 00:00:00 2001 From: pyz4 <42039243+pyz4@users.noreply.github.com> Date: Fri, 18 Apr 2025 16:48:59 -0400 Subject: [PATCH] feat(polars): add pow (`**`) operator for polars expressions (#15598) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description This PR adds the exponent operator ("**") to polars expressions. ```nushell > [[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars select a b {c: ((polars col a) ** 2)} ╭───┬───┬───┬────╮ │ # │ a │ b │ c │ ├───┼───┼───┼────┤ │ 0 │ 6 │ 2 │ 36 │ │ 1 │ 4 │ 2 │ 16 │ │ 2 │ 2 │ 2 │ 4 │ ╰───┴───┴───┴────╯ ``` # User-Facing Changes No breaking changes. Users are enabled to use the `**` operator in polars expressions. # Tests + Formatting An example in `polars select` was modified to showcase the `**` operator. # After Submitting --- crates/nu_plugin_polars/src/dataframe/command/data/select.rs | 4 ++-- .../src/dataframe/values/nu_expression/custom_value.rs | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/nu_plugin_polars/src/dataframe/command/data/select.rs b/crates/nu_plugin_polars/src/dataframe/command/data/select.rs index 8c9a135f4b..6bcadb9d2b 100644 --- a/crates/nu_plugin_polars/src/dataframe/command/data/select.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/select.rs @@ -72,7 +72,7 @@ impl PluginCommand for LazySelect { }, Example { description: "Select a column from a dataframe using a mix of expressions and record of expressions", - example: "[[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars select a b {c: ((polars col a) * 2)}", + example: "[[a b]; [6 2] [4 2] [2 2]] | polars into-df | polars select a b {c: ((polars col a) ** 2)}", result: Some( NuDataFrame::try_from_columns( vec![ @@ -84,7 +84,7 @@ impl PluginCommand for LazySelect { vec![Value::test_int(2), Value::test_int(2), Value::test_int(2)]), Column::new( "c".to_string(), - vec![Value::test_int(12), Value::test_int(8), Value::test_int(4)]) + vec![Value::test_int(36), Value::test_int(16), Value::test_int(4)]) ], None, ) diff --git a/crates/nu_plugin_polars/src/dataframe/values/nu_expression/custom_value.rs b/crates/nu_plugin_polars/src/dataframe/values/nu_expression/custom_value.rs index b5a918f287..dc1db125ef 100644 --- a/crates/nu_plugin_polars/src/dataframe/values/nu_expression/custom_value.rs +++ b/crates/nu_plugin_polars/src/dataframe/values/nu_expression/custom_value.rs @@ -103,6 +103,9 @@ fn with_operator( Operator::Math(Math::FloorDivide) => { apply_arithmetic(plugin, engine, left, right, lhs_span, Div::div) } + Operator::Math(Math::Pow) => { + apply_arithmetic(plugin, engine, left, right, lhs_span, Expr::pow) + } Operator::Comparison(Comparison::Equal) => Ok(left .clone() .apply_with_expr(right.clone(), Expr::eq)