From 21d949207fb70ad7edba28216780dd77fc122316 Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Thu, 5 Jun 2025 13:53:22 -0700 Subject: [PATCH] Add regex documentation/examples to `polars col` (#15898) # Description Include regular expression example and help documentation to `polars col` Co-authored-by: Jack Wright --- .../src/dataframe/command/data/col.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/nu_plugin_polars/src/dataframe/command/data/col.rs b/crates/nu_plugin_polars/src/dataframe/command/data/col.rs index 0432262028..a40c9a99ec 100644 --- a/crates/nu_plugin_polars/src/dataframe/command/data/col.rs +++ b/crates/nu_plugin_polars/src/dataframe/command/data/col.rs @@ -8,7 +8,7 @@ use nu_protocol::{ Category, Example, LabeledError, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value, record, }; -use polars::prelude::DataType; +use polars::{df, prelude::DataType}; #[derive(Clone)] pub struct ExprCol; @@ -29,7 +29,7 @@ impl PluginCommand for ExprCol { .required( "column name", SyntaxShape::String, - "Name of column to be used. '*' can be used for all columns.", + "Name of column to be used. '*' can be used for all columns. Accepts regular expression input; regular expressions should start with ^ and end with $.", ) .rest( "more columns", @@ -107,6 +107,21 @@ impl PluginCommand for ExprCol { .into_value(Span::test_data()), ), }, + Example { + description: "Select columns using a regular expression", + example: "[[ham hamburger foo bar]; [1 11 2 a] [2 22 1 b]] | polars into-df | polars select (polars col '^ham.*$') | polars collect", + result: Some( + NuDataFrame::new( + false, + df!( + "ham" => [1, 2], + "hamburger" => [11, 22], + ) + .expect("should not fail to create dataframe"), + ) + .into_value(Span::test_data()), + ), + }, ] }