Selectors#
Selectors allow for more intuitive selection of columns from DataFrame
or LazyFrame objects based on their name, dtype or other properties.
They unify and build on the related functionality that is available through
the col() expression and can also broadcast expressions over the selected
columns.
Importing#
Selectors are available as functions imported from
polars.selectorsTypical/recommended usage is to import the module as
csand employ selectors from there.import polars.selectors as cs import polars as pl df = pl.DataFrame( { "w": ["xx", "yy", "xx", "yy", "xx"], "x": [1, 2, 1, 4, -2], "y": [3.0, 4.5, 1.0, 2.5, -2.0], "z": ["a", "b", "a", "b", "b"], }, ) df.group_by(cs.string()).agg(cs.numeric().sum())
Set operations#
Selectors support the following set operations:
Operation |
Expression |
|---|---|
|
|
|
|
|
|
|
|
|
|
Note that both individual selector results and selector set operations will always return matching columns in the same order as the underlying frame schema.
Examples#
import polars.selectors as cs
import polars as pl
# set up an empty dataframe with plenty of columns of various dtypes
df = pl.DataFrame(
schema={
"abc": pl.UInt16,
"bbb": pl.UInt32,
"cde": pl.Float64,
"def": pl.Float32,
"eee": pl.Boolean,
"fgg": pl.Boolean,
"ghi": pl.Time,
"JJK": pl.Date,
"Lmn": pl.Duration,
"opp": pl.Datetime("ms"),
"qqR": pl.String,
},
)
# Select the UNION of temporal, strings and columns that start with "e"
assert df.select(cs.temporal() | cs.string() | cs.starts_with("e")).schema == {
"eee": pl.Boolean,
"ghi": pl.Time,
"JJK": pl.Date,
"Lmn": pl.Duration,
"opp": pl.Datetime("ms"),
"qqR": pl.String,
}
# Select the INTERSECTION of temporal and column names that match "opp" OR "JJK"
assert df.select(cs.temporal() & cs.matches("opp|JJK")).schema == {
"JJK": pl.Date,
"opp": pl.Datetime("ms"),
}
# Select the DIFFERENCE of temporal columns and columns that contain the name "opp" OR "JJK"
assert df.select(cs.temporal() - cs.matches("opp|JJK")).schema == {
"ghi": pl.Time,
"Lmn": pl.Duration,
}
# Select the SYMMETRIC DIFFERENCE of numeric columns and columns that contain an "e"
assert df.select(cs.contains("e") ^ cs.numeric()).schema == {
"abc": UInt16,
"bbb": UInt32,
"eee": Boolean,
}
# Select the COMPLEMENT of all columns of dtypes Duration and Time
assert df.select(~cs.by_dtype([pl.Duration, pl.Time])).schema == {
"abc": pl.UInt16,
"bbb": pl.UInt32,
"cde": pl.Float64,
"def": pl.Float32,
"eee": pl.Boolean,
"fgg": pl.Boolean,
"JJK": pl.Date,
"opp": pl.Datetime("ms"),
"qqR": pl.String,
}
Note
If you donโt want to use the set operations on the selectors, you can materialize them as expressions
by calling as_expr. This ensures the operations OR, AND, etc are dispatched to the underlying
expressions instead.
Functions#
Available selector functions:
|
Base column selector expression/proxy. |
|
Select all columns. |
|
Select all columns with alphabetic names (eg: only letters). |
|
Select all columns with alphanumeric names (eg: only letters and the digits 0-9). |
|
Select all array columns. |
|
Select all binary columns. |
|
Select all boolean columns. |
|
Select all columns matching the given dtypes. |
|
Select all columns matching the given indices (or range objects). |
|
Select all columns matching the given names. |
Select all categorical columns. |
|
|
Select columns whose names contain the given literal substring(s). |
|
Select all date columns. |
|
Select all datetime columns, optionally filtering by time unit/zone. |
|
Select all decimal columns. |
|
Select all columns having names consisting only of digits. |
|
Select all duration columns, optionally filtering by time unit. |
|
Select columns that end with the given substring(s). |
|
Select all enum columns. |
|
Select all columns except those matching the given columns, datatypes, or selectors. |
|
Expand selector to column names, with respect to a specific frame or target schema. |
|
Select the first column in the current scope. |
|
Select all float columns. |
|
Select all integer columns. |
|
Indicate whether the given object/expression is a selector. |
|
Select the last column in the current scope. |
|
Select all list columns. |
|
Select all map columns. |
|
Select all columns that match the given regex pattern. |
|
Select all nested columns. |
|
Select all numeric columns. |
Select all signed integer columns. |
|
|
Select columns that start with the given substring(s). |
|
Select all String (and, optionally, Categorical) string columns. |
|
Select all struct columns. |
|
Select all temporal columns. |
|
Select all time columns. |
Select all unsigned integer columns. |
- class polars.selectors.Selector[source]#
Base column selector expression/proxy.
as_expr()Materialize the
selectoras a normal expression.exclude(columns,ย *more_columns)Exclude columns from a multi-column expression.
- as_expr() Expr[source]#
Materialize the
selectoras a normal expression.This ensures that the operators
|,&,~and-are applied on the data and not on the selector sets.Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "colx": ["aa", "bb", "cc"], ... "coly": [True, False, True], ... "colz": [1, 2, 3], ... } ... )
Inverting the boolean selector will choose the non-boolean columns:
>>> df.select(~cs.boolean()) shape: (3, 2) โโโโโโโโฌโโโโโโโ โ colx โ colz โ โ --- โ --- โ โ str โ i64 โ โโโโโโโโชโโโโโโโก โ aa โ 1 โ โ bb โ 2 โ โ cc โ 3 โ โโโโโโโโดโโโโโโโ
To invert the values in the selected boolean columns, we need to materialize the selector as a standard expression instead:
>>> df.select(~cs.boolean().as_expr()) shape: (3, 1) โโโโโโโโโ โ coly โ โ --- โ โ bool โ โโโโโโโโโก โ false โ โ true โ โ false โ โโโโโโโโโ
- exclude(
- columns: str | PolarsDataType | Collection[str | PolarsDataType],
- *more_columns: str | PolarsDataType,
Exclude columns from a multi-column expression.
Only works after a wildcard or regex column selection, and you cannot provide both string column names and dtypes (you may prefer to use selectors instead).
- Parameters:
- columns
The name or datatype of the column(s) to exclude. Accepts regular expression input. Regular expressions should start with
^and end with$.- *more_columns
Additional names or datatypes of columns to exclude, specified as positional arguments.
- polars.selectors.all() Selector[source]#
Select all columns.
See also
Examples
>>> from datetime import date >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "dt": [date(1999, 12, 31), date(2024, 1, 1)], ... "value": [1_234_500, 5_000_555], ... }, ... schema_overrides={"value": pl.Int32}, ... )
Select all columns, casting them to string:
>>> df.select(cs.all().cast(pl.String)) shape: (2, 2) โโโโโโโโโโโโโโฌโโโโโโโโโโ โ dt โ value โ โ --- โ --- โ โ str โ str โ โโโโโโโโโโโโโโชโโโโโโโโโโก โ 1999-12-31 โ 1234500 โ โ 2024-01-01 โ 5000555 โ โโโโโโโโโโโโโโดโโโโโโโโโโ
Select all columns except for those matching the given dtypes:
>>> df.select(cs.all() - cs.numeric()) shape: (2, 1) โโโโโโโโโโโโโโ โ dt โ โ --- โ โ date โ โโโโโโโโโโโโโโก โ 1999-12-31 โ โ 2024-01-01 โ โโโโโโโโโโโโโโ
- polars.selectors.alpha( ) Selector[source]#
Select all columns with alphabetic names (eg: only letters).
- Parameters:
- ascii_only
Indicate whether to consider only ASCII alphabetic characters, or the full Unicode range of valid letters (accented, idiographic, etc).
- ignore_spaces
Indicate whether to ignore the presence of spaces in column names; if so, only the other (non-space) characters are considered.
Notes
Matching column names cannot contain any non-alphabetic characters. Note that the definition of โalphabeticโ consists of all valid Unicode alphabetic characters (
p{Alphabetic}) by default; this can be changed by settingascii_only=True.Examples
>>> import polars as pl >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "no1": [100, 200, 300], ... "cafรฉ": ["espresso", "latte", "mocha"], ... "t or f": [True, False, None], ... "hmm": ["aaa", "bbb", "ccc"], ... "้ฝๅธ": ["ๆฑไบฌ", "ๅคง้ช", "ไบฌ้ฝ"], ... } ... )
Select columns with alphabetic names; note that accented characters and kanji are recognised as alphabetic here:
>>> df.select(cs.alpha()) shape: (3, 3) โโโโโโโโโโโโฌโโโโโโฌโโโโโโโ โ cafรฉ โ hmm โ ้ฝๅธ โ โ --- โ --- โ --- โ โ str โ str โ str โ โโโโโโโโโโโโชโโโโโโชโโโโโโโก โ espresso โ aaa โ ๆฑไบฌ โ โ latte โ bbb โ ๅคง้ช โ โ mocha โ ccc โ ไบฌ้ฝ โ โโโโโโโโโโโโดโโโโโโดโโโโโโโ
Constrain the definition of โalphabeticโ to ASCII characters only:
>>> df.select(cs.alpha(ascii_only=True)) shape: (3, 1) โโโโโโโ โ hmm โ โ --- โ โ str โ โโโโโโโก โ aaa โ โ bbb โ โ ccc โ โโโโโโโ
>>> df.select(cs.alpha(ascii_only=True, ignore_spaces=True)) shape: (3, 2) โโโโโโโโโโฌโโโโโโ โ t or f โ hmm โ โ --- โ --- โ โ bool โ str โ โโโโโโโโโโชโโโโโโก โ true โ aaa โ โ false โ bbb โ โ null โ ccc โ โโโโโโโโโโดโโโโโโ
Select all columns except for those with alphabetic names:
>>> df.select(~cs.alpha()) shape: (3, 2) โโโโโโโฌโโโโโโโโโ โ no1 โ t or f โ โ --- โ --- โ โ i64 โ bool โ โโโโโโโชโโโโโโโโโก โ 100 โ true โ โ 200 โ false โ โ 300 โ null โ โโโโโโโดโโโโโโโโโ
>>> df.select(~cs.alpha(ignore_spaces=True)) shape: (3, 1) โโโโโโโ โ no1 โ โ --- โ โ i64 โ โโโโโโโก โ 100 โ โ 200 โ โ 300 โ โโโโโโโ
- polars.selectors.alphanumeric( ) Selector[source]#
Select all columns with alphanumeric names (eg: only letters and the digits 0-9).
- Parameters:
- ascii_only
Indicate whether to consider only ASCII alphabetic characters, or the full Unicode range of valid letters (accented, idiographic, etc).
- ignore_spaces
Indicate whether to ignore the presence of spaces in column names; if so, only the other (non-space) characters are considered.
Notes
Matching column names cannot contain any non-alphabetic or integer characters. Note that the definition of โalphabeticโ consists of all valid Unicode alphabetic characters (
p{Alphabetic}) and digit characters (d) by default; this can be changed by settingascii_only=True.Examples
>>> import polars as pl >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "1st_col": [100, 200, 300], ... "flagged": [True, False, True], ... "00prefix": ["01:aa", "02:bb", "03:cc"], ... "last col": ["x", "y", "z"], ... } ... )
Select columns with alphanumeric names:
>>> df.select(cs.alphanumeric()) shape: (3, 2) โโโโโโโโโโโฌโโโโโโโโโโโ โ flagged โ 00prefix โ โ --- โ --- โ โ bool โ str โ โโโโโโโโโโโชโโโโโโโโโโโก โ true โ 01:aa โ โ false โ 02:bb โ โ true โ 03:cc โ โโโโโโโโโโโดโโโโโโโโโโโ
>>> df.select(cs.alphanumeric(ignore_spaces=True)) shape: (3, 3) โโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโ โ flagged โ 00prefix โ last col โ โ --- โ --- โ --- โ โ bool โ str โ str โ โโโโโโโโโโโชโโโโโโโโโโโชโโโโโโโโโโโก โ true โ 01:aa โ x โ โ false โ 02:bb โ y โ โ true โ 03:cc โ z โ โโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโ
Select all columns except for those with alphanumeric names:
>>> df.select(~cs.alphanumeric()) shape: (3, 2) โโโโโโโโโโโฌโโโโโโโโโโโ โ 1st_col โ last col โ โ --- โ --- โ โ i64 โ str โ โโโโโโโโโโโชโโโโโโโโโโโก โ 100 โ x โ โ 200 โ y โ โ 300 โ z โ โโโโโโโโโโโดโโโโโโโโโโโ
>>> df.select(~cs.alphanumeric(ignore_spaces=True)) shape: (3, 1) โโโโโโโโโโโ โ 1st_col โ โ --- โ โ i64 โ โโโโโโโโโโโก โ 100 โ โ 200 โ โ 300 โ โโโโโโโโโโโ
- polars.selectors.array( ) Selector[source]#
Select all array columns.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [["xx", "yy"], ["x", "y"]], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... }, ... schema_overrides={"foo": pl.Array(pl.String, 2)}, ... )
Select all array columns:
>>> df.select(cs.array()) shape: (2, 1) โโโโโโโโโโโโโโโโโ โ foo โ โ --- โ โ array[str, 2] โ โโโโโโโโโโโโโโโโโก โ ["xx", "yy"] โ โ ["x", "y"] โ โโโโโโโโโโโโโโโโโ
Select all columns except for those that are array:
>>> df.select(~cs.array()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
Select all array columns with a certain matching inner type:
>>> df.select(cs.array(cs.string())) shape: (2, 1) โโโโโโโโโโโโโโโโโ โ foo โ โ --- โ โ array[str, 2] โ โโโโโโโโโโโโโโโโโก โ ["xx", "yy"] โ โ ["x", "y"] โ โโโโโโโโโโโโโโโโโ >>> df.select(cs.array(cs.integer())) shape: (0, 0) โโ โโก โโ >>> df.select(cs.array(width=2)) shape: (2, 1) โโโโโโโโโโโโโโโโโ โ foo โ โ --- โ โ array[str, 2] โ โโโโโโโโโโโโโโโโโก โ ["xx", "yy"] โ โ ["x", "y"] โ โโโโโโโโโโโโโโโโโ >>> df.select(cs.array(width=3)) shape: (0, 0) โโ โโก โโ
- polars.selectors.binary() Selector[source]#
Select all binary columns.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame({"a": [b"hello"], "b": ["world"], "c": [b"!"], "d": [":)"]}) >>> df shape: (1, 4) โโโโโโโโโโโโฌโโโโโโโโฌโโโโโโโโโฌโโโโโโ โ a โ b โ c โ d โ โ --- โ --- โ --- โ --- โ โ binary โ str โ binary โ str โ โโโโโโโโโโโโชโโโโโโโโชโโโโโโโโโชโโโโโโก โ b"hello" โ world โ b"!" โ :) โ โโโโโโโโโโโโดโโโโโโโโดโโโโโโโโโดโโโโโโ
Select binary columns and export as a dict:
>>> df.select(cs.binary()).to_dict(as_series=False) {'a': [b'hello'], 'c': [b'!']}
Select all columns except for those that are binary:
>>> df.select(~cs.binary()).to_dict(as_series=False) {'b': ['world'], 'd': [':)']}
- polars.selectors.boolean() Selector[source]#
Select all boolean columns.
See also
by_dtypeSelect all columns matching the given dtype(s).
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame({"n": range(1, 5)}).with_columns(n_even=pl.col("n") % 2 == 0) >>> df shape: (4, 2) โโโโโโโฌโโโโโโโโโ โ n โ n_even โ โ --- โ --- โ โ i64 โ bool โ โโโโโโโชโโโโโโโโโก โ 1 โ false โ โ 2 โ true โ โ 3 โ false โ โ 4 โ true โ โโโโโโโดโโโโโโโโโ
Select and invert boolean columns:
>>> df.with_columns(is_odd=cs.boolean().not_()) shape: (4, 3) โโโโโโโฌโโโโโโโโโฌโโโโโโโโโ โ n โ n_even โ is_odd โ โ --- โ --- โ --- โ โ i64 โ bool โ bool โ โโโโโโโชโโโโโโโโโชโโโโโโโโโก โ 1 โ false โ true โ โ 2 โ true โ false โ โ 3 โ false โ true โ โ 4 โ true โ false โ โโโโโโโดโโโโโโโโโดโโโโโโโโโ
Select all columns except for those that are boolean:
>>> df.select(~cs.boolean()) shape: (4, 1) โโโโโโโ โ n โ โ --- โ โ i64 โ โโโโโโโก โ 1 โ โ 2 โ โ 3 โ โ 4 โ โโโโโโโ
- polars.selectors.by_dtype(
- *dtypes: PolarsDataType | PythonDataType | Iterable[PolarsDataType] | Iterable[PythonDataType],
Select all columns matching the given dtypes.
See also
Examples
>>> from datetime import date >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "dt": [date(1999, 12, 31), date(2024, 1, 1), date(2010, 7, 5)], ... "value": [1_234_500, 5_000_555, -4_500_000], ... "other": ["foo", "bar", "foo"], ... } ... )
Select all columns with date or string dtypes:
>>> df.select(cs.by_dtype(pl.Date, pl.String)) shape: (3, 2) โโโโโโโโโโโโโโฌโโโโโโโโ โ dt โ other โ โ --- โ --- โ โ date โ str โ โโโโโโโโโโโโโโชโโโโโโโโก โ 1999-12-31 โ foo โ โ 2024-01-01 โ bar โ โ 2010-07-05 โ foo โ โโโโโโโโโโโโโโดโโโโโโโโ
Select all columns that are not of date or string dtype:
>>> df.select(~cs.by_dtype(pl.Date, pl.String)) shape: (3, 1) โโโโโโโโโโโโ โ value โ โ --- โ โ i64 โ โโโโโโโโโโโโก โ 1234500 โ โ 5000555 โ โ -4500000 โ โโโโโโโโโโโโ
Group by string columns and sum the numeric columns:
>>> df.group_by(cs.string()).agg(cs.numeric().sum()).sort(by="other") shape: (2, 2) โโโโโโโโโฌโโโโโโโโโโโ โ other โ value โ โ --- โ --- โ โ str โ i64 โ โโโโโโโโโชโโโโโโโโโโโก โ bar โ 5000555 โ โ foo โ -3265500 โ โโโโโโโโโดโโโโโโโโโโโ
- polars.selectors.by_index( ) Selector[source]#
Select all columns matching the given indices (or range objects).
- Parameters:
- *indices
One or more column indices (or range objects). Negative indexing is supported.
- require_all
By default, all specified indices must be valid; if any index is out of bounds, an error is raised. If set to
False, out-of-bounds indices are ignored
See also
Notes
Matching columns are returned in the order in which their indexes appear in the selector, not the underlying schema order.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "key": ["abc"], ... **{f"c{i:02}": [0.5 * i] for i in range(100)}, ... }, ... ) >>> print(df) shape: (1, 101) โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโ โ key โ c00 โ c01 โ c02 โ โฆ โ c96 โ c97 โ c98 โ c99 โ โ --- โ --- โ --- โ --- โ โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ โ f64 โ f64 โ f64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโก โ abc โ 0.0 โ 0.5 โ 1.0 โ โฆ โ 48.0 โ 48.5 โ 49.0 โ 49.5 โ โโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโ
Select columns by index (โkeyโ column and the two first/last columns):
>>> df.select(cs.by_index(0, 1, 2, -2, -1)) shape: (1, 5) โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโโฌโโโโโโโ โ key โ c00 โ c01 โ c98 โ c99 โ โ --- โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโชโโโโโโโชโโโโโโโก โ abc โ 0.0 โ 0.5 โ 49.0 โ 49.5 โ โโโโโโโดโโโโโโดโโโโโโดโโโโโโโดโโโโโโโ
Select the โkeyโ column and use a
rangeobject to select various columns. Note that you can freely mix and match integer indices andrangeobjects:>>> df.select(cs.by_index(0, range(1, 101, 20))) shape: (1, 6) โโโโโโโฌโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโ โ key โ c00 โ c20 โ c40 โ c60 โ c80 โ โ --- โ --- โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ f64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโก โ abc โ 0.0 โ 10.0 โ 20.0 โ 30.0 โ 40.0 โ โโโโโโโดโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโ
>>> df.select(cs.by_index(0, range(101, 0, -25), require_all=False)) shape: (1, 5) โโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโ โ key โ c75 โ c50 โ c25 โ c00 โ โ --- โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ f64 โ โโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโก โ abc โ 37.5 โ 25.0 โ 12.5 โ 0.0 โ โโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโ
Select all columns except for the even-indexed ones:
>>> df.select(~cs.by_index(range(1, 100, 2))) shape: (1, 51) โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโฌโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโ โ key โ c01 โ c03 โ c05 โ โฆ โ c93 โ c95 โ c97 โ c99 โ โ --- โ --- โ --- โ --- โ โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ โ f64 โ f64 โ f64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโชโโโโโโชโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโก โ abc โ 0.5 โ 1.5 โ 2.5 โ โฆ โ 46.5 โ 47.5 โ 48.5 โ 49.5 โ โโโโโโโดโโโโโโดโโโโโโดโโโโโโดโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโ
- polars.selectors.by_name(
- *names: str | Collection[str],
- require_all: bool = True,
Select all columns matching the given names.
Added in version 0.20.27: The
require_allparameter was added.- Parameters:
- *names
One or more names of columns to select.
- require_all
Whether to match all names (the default) or any of the names.
See also
Notes
Matching columns are returned in the order in which they are declared in the selector, not the underlying schema order.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [False, True], ... } ... )
Select columns by name:
>>> df.select(cs.by_name("foo", "bar")) shape: (2, 2) โโโโโโโฌโโโโโโ โ foo โ bar โ โ --- โ --- โ โ str โ i64 โ โโโโโโโชโโโโโโก โ x โ 123 โ โ y โ 456 โ โโโโโโโดโโโโโโ
Match any of the given columns by name:
>>> df.select(cs.by_name("baz", "moose", "foo", "bear", require_all=False)) shape: (2, 2) โโโโโโโฌโโโโโโ โ baz โ foo โ โ --- โ --- โ โ f64 โ str โ โโโโโโโชโโโโโโก โ 2.0 โ x โ โ 5.5 โ y โ โโโโโโโดโโโโโโ
Match all columns except for those given:
>>> df.select(~cs.by_name("foo", "bar")) shape: (2, 2) โโโโโโโฌโโโโโโโโ โ baz โ zap โ โ --- โ --- โ โ f64 โ bool โ โโโโโโโชโโโโโโโโก โ 2.0 โ false โ โ 5.5 โ true โ โโโโโโโดโโโโโโโโ
- polars.selectors.categorical() Selector[source]#
Select all categorical columns.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["xx", "yy"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... }, ... schema_overrides={"foo": pl.Categorical}, ... )
Select all categorical columns:
>>> df.select(cs.categorical()) shape: (2, 1) โโโโโโโ โ foo โ โ --- โ โ cat โ โโโโโโโก โ xx โ โ yy โ โโโโโโโ
Select all columns except for those that are categorical:
>>> df.select(~cs.categorical()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
- polars.selectors.contains(*substring: str) Selector[source]#
Select columns whose names contain the given literal substring(s).
- Parameters:
- substring
Substring(s) that matching column names should contain.
See also
matchesSelect all columns that match the given regex pattern.
ends_withSelect columns that end with the given substring(s).
starts_withSelect columns that start with the given substring(s).
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [False, True], ... } ... )
Select columns that contain the substring โbaโ:
>>> df.select(cs.contains("ba")) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
Select columns that contain the substring โbaโ or the letter โzโ:
>>> df.select(cs.contains("ba", "z")) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโโโ โ bar โ baz โ zap โ โ --- โ --- โ --- โ โ i64 โ f64 โ bool โ โโโโโโโชโโโโโโชโโโโโโโโก โ 123 โ 2.0 โ false โ โ 456 โ 5.5 โ true โ โโโโโโโดโโโโโโดโโโโโโโโ
Select all columns except for those that contain the substring โbaโ:
>>> df.select(~cs.contains("ba")) shape: (2, 2) โโโโโโโฌโโโโโโโโ โ foo โ zap โ โ --- โ --- โ โ str โ bool โ โโโโโโโชโโโโโโโโก โ x โ false โ โ y โ true โ โโโโโโโดโโโโโโโโ
- polars.selectors.date() Selector[source]#
Select all date columns.
See also
Examples
>>> from datetime import date, datetime, time >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "dtm": [datetime(2001, 5, 7, 10, 25), datetime(2031, 12, 31, 0, 30)], ... "dt": [date(1999, 12, 31), date(2024, 8, 9)], ... "tm": [time(0, 0, 0), time(23, 59, 59)], ... }, ... )
Select all date columns:
>>> df.select(cs.date()) shape: (2, 1) โโโโโโโโโโโโโโ โ dt โ โ --- โ โ date โ โโโโโโโโโโโโโโก โ 1999-12-31 โ โ 2024-08-09 โ โโโโโโโโโโโโโโ
Select all columns except for those that are dates:
>>> df.select(~cs.date()) shape: (2, 2) โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ โ dtm โ tm โ โ --- โ --- โ โ datetime[ฮผs] โ time โ โโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโก โ 2001-05-07 10:25:00 โ 00:00:00 โ โ 2031-12-31 00:30:00 โ 23:59:59 โ โโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโ
- polars.selectors.datetime(
- time_unit: TimeUnit | Collection[TimeUnit] | None = None,
- time_zone: str | pytimezone | Collection[str | pytimezone | None] | None = ('*', None),
Select all datetime columns, optionally filtering by time unit/zone.
- Parameters:
- time_unit
One (or more) of the allowed timeunit precision strings, โmsโ, โusโ, and โnsโ. Omit to select columns with any valid timeunit.
- time_zone
One or more timezone strings, as defined in zoneinfo (to see valid options run
import zoneinfo; zoneinfo.available_timezones()for a full list).Set
Noneto select Datetime columns that do not have a timezone.Set โ*โ to select Datetime columns that have any timezone.
See also
Examples
>>> from datetime import datetime, date, timezone >>> import polars.selectors as cs >>> from zoneinfo import ZoneInfo >>> tokyo_tz = ZoneInfo("Asia/Tokyo") >>> utc_tz = timezone.utc >>> df = pl.DataFrame( ... { ... "tstamp_tokyo": [ ... datetime(1999, 7, 21, 5, 20, 16, 987654, tzinfo=tokyo_tz), ... datetime(2000, 5, 16, 6, 21, 21, 123465, tzinfo=tokyo_tz), ... ], ... "tstamp_utc": [ ... datetime(2023, 4, 10, 12, 14, 16, 999000, tzinfo=utc_tz), ... datetime(2025, 8, 25, 14, 18, 22, 666000, tzinfo=utc_tz), ... ], ... "tstamp": [ ... datetime(2000, 11, 20, 18, 12, 16, 600000), ... datetime(2020, 10, 30, 10, 20, 25, 123000), ... ], ... "dt": [date(1999, 12, 31), date(2010, 7, 5)], ... }, ... schema_overrides={ ... "tstamp_tokyo": pl.Datetime("ns", "Asia/Tokyo"), ... "tstamp_utc": pl.Datetime("us", "UTC"), ... }, ... )
Select all datetime columns:
>>> df.select(cs.datetime()) shape: (2, 3) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ โ tstamp_tokyo โ tstamp_utc โ tstamp โ โ --- โ --- โ --- โ โ datetime[ns, Asia/Tokyo] โ datetime[ฮผs, UTC] โ datetime[ฮผs] โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโโโโโโก โ 1999-07-21 05:20:16.987654 JST โ 2023-04-10 12:14:16.999 UTC โ 2000-11-20 18:12:16.600 โ โ 2000-05-16 06:21:21.123465 JST โ 2025-08-25 14:18:22.666 UTC โ 2020-10-30 10:20:25.123 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโ
Select all datetime columns that have โusโ precision:
>>> df.select(cs.datetime("us")) shape: (2, 2) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ โ tstamp_utc โ tstamp โ โ --- โ --- โ โ datetime[ฮผs, UTC] โ datetime[ฮผs] โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโโโโโโก โ 2023-04-10 12:14:16.999 UTC โ 2000-11-20 18:12:16.600 โ โ 2025-08-25 14:18:22.666 UTC โ 2020-10-30 10:20:25.123 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโ
Select all datetime columns that have any timezone:
>>> df.select(cs.datetime(time_zone="*")) shape: (2, 2) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ tstamp_tokyo โ tstamp_utc โ โ --- โ --- โ โ datetime[ns, Asia/Tokyo] โ datetime[ฮผs, UTC] โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโก โ 1999-07-21 05:20:16.987654 JST โ 2023-04-10 12:14:16.999 UTC โ โ 2000-05-16 06:21:21.123465 JST โ 2025-08-25 14:18:22.666 UTC โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Select all datetime columns that have a specific timezone:
>>> df.select(cs.datetime(time_zone="UTC")) shape: (2, 1) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ tstamp_utc โ โ --- โ โ datetime[ฮผs, UTC] โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโก โ 2023-04-10 12:14:16.999 UTC โ โ 2025-08-25 14:18:22.666 UTC โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Select all datetime columns that have NO timezone:
>>> df.select(cs.datetime(time_zone=None)) shape: (2, 1) โโโโโโโโโโโโโโโโโโโโโโโโโโโ โ tstamp โ โ --- โ โ datetime[ฮผs] โ โโโโโโโโโโโโโโโโโโโโโโโโโโโก โ 2000-11-20 18:12:16.600 โ โ 2020-10-30 10:20:25.123 โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
Select all columns except for datetime columns:
>>> df.select(~cs.datetime()) shape: (2, 1) โโโโโโโโโโโโโโ โ dt โ โ --- โ โ date โ โโโโโโโโโโโโโโก โ 1999-12-31 โ โ 2010-07-05 โ โโโโโโโโโโโโโโ
- polars.selectors.decimal() Selector[source]#
Select all decimal columns.
See also
Examples
>>> from decimal import Decimal as D >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [D(123), D(456)], ... "baz": [D("2.0005"), D("-50.5555")], ... }, ... schema_overrides={"baz": pl.Decimal(scale=5, precision=10)}, ... )
Select all decimal columns:
>>> df.select(cs.decimal()) shape: (2, 2) โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ โ bar โ baz โ โ --- โ --- โ โ decimal[38,0] โ decimal[10,5] โ โโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโก โ 123 โ 2.00050 โ โ 456 โ -50.55550 โ โโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ
Select all columns except the decimal ones:
>>> df.select(~cs.decimal()) shape: (2, 1) โโโโโโโ โ foo โ โ --- โ โ str โ โโโโโโโก โ x โ โ y โ โโโโโโโ
- polars.selectors.digit(ascii_only: bool = False) Selector[source]#
Select all columns having names consisting only of digits.
Notes
Matching column names cannot contain any non-digit characters. Note that the definition of โdigitโ consists of all valid Unicode digit characters (
d) by default; this can be changed by settingascii_only=True.Examples
>>> import polars as pl >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "key": ["aaa", "bbb", "aaa", "bbb", "bbb"], ... "year": [2001, 2001, 2025, 2025, 2001], ... "value": [-25, 100, 75, -15, -5], ... } ... ).pivot( ... values="value", ... index="key", ... on="year", ... aggregate_function="sum", ... ) >>> print(df) shape: (2, 3) โโโโโโโฌโโโโโโโฌโโโโโโโ โ key โ 2001 โ 2025 โ โ --- โ --- โ --- โ โ str โ i64 โ i64 โ โโโโโโโชโโโโโโโชโโโโโโโก โ aaa โ -25 โ 75 โ โ bbb โ 95 โ -15 โ โโโโโโโดโโโโโโโดโโโโโโโ
Select columns with digit names:
>>> df.select(cs.digit()) shape: (2, 2) โโโโโโโโฌโโโโโโโ โ 2001 โ 2025 โ โ --- โ --- โ โ i64 โ i64 โ โโโโโโโโชโโโโโโโก โ -25 โ 75 โ โ 95 โ -15 โ โโโโโโโโดโโโโโโโ
Select all columns except for those with digit names:
>>> df.select(~cs.digit()) shape: (2, 1) โโโโโโโ โ key โ โ --- โ โ str โ โโโโโโโก โ aaa โ โ bbb โ โโโโโโโ
Demonstrate use of
ascii_onlyflag (by default all valid unicode digits are considered, but this can be constrained to ascii 0-9):>>> df = pl.DataFrame({"เฅงเฅฏเฅฏเฅฏ": [1999], "เฅจเฅฆเฅญเฅญ": [2077], "3000": [3000]}) >>> df.select(cs.digit()) shape: (1, 3) โโโโโโโโฌโโโโโโโฌโโโโโโโ โ เฅงเฅฏเฅฏเฅฏ โ เฅจเฅฆเฅญเฅญ โ 3000 โ โ --- โ --- โ --- โ โ i64 โ i64 โ i64 โ โโโโโโโโชโโโโโโโชโโโโโโโก โ 1999 โ 2077 โ 3000 โ โโโโโโโโดโโโโโโโดโโโโโโโ
>>> df.select(cs.digit(ascii_only=True)) shape: (1, 1) โโโโโโโโ โ 3000 โ โ --- โ โ i64 โ โโโโโโโโก โ 3000 โ โโโโโโโโ
- polars.selectors.duration(time_unit: TimeUnit | Collection[TimeUnit] | None = None) Selector[source]#
Select all duration columns, optionally filtering by time unit.
- Parameters:
- time_unit
One (or more) of the allowed timeunit precision strings, โmsโ, โusโ, and โnsโ. Omit to select columns with any valid timeunit.
See also
Examples
>>> from datetime import date, timedelta >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "dt": [date(2022, 1, 31), date(2025, 7, 5)], ... "td1": [ ... timedelta(days=1, milliseconds=123456), ... timedelta(days=1, hours=23, microseconds=987000), ... ], ... "td2": [ ... timedelta(days=7, microseconds=456789), ... timedelta(days=14, minutes=999, seconds=59), ... ], ... "td3": [ ... timedelta(weeks=4, days=-10, microseconds=999999), ... timedelta(weeks=3, milliseconds=123456, microseconds=1), ... ], ... }, ... schema_overrides={ ... "td1": pl.Duration("ms"), ... "td2": pl.Duration("us"), ... "td3": pl.Duration("ns"), ... }, ... )
Select all duration columns:
>>> df.select(cs.duration()) shape: (2, 3) โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ โ td1 โ td2 โ td3 โ โ --- โ --- โ --- โ โ duration[ms] โ duration[ฮผs] โ duration[ns] โ โโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโก โ 1d 2m 3s 456ms โ 7d 456789ยตs โ 18d 999999ยตs โ โ 1d 23h 987ms โ 14d 16h 39m 59s โ 21d 2m 3s 456001ยตs โ โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโ
Select all duration columns that have โmsโ precision:
>>> df.select(cs.duration("ms")) shape: (2, 1) โโโโโโโโโโโโโโโโโโ โ td1 โ โ --- โ โ duration[ms] โ โโโโโโโโโโโโโโโโโโก โ 1d 2m 3s 456ms โ โ 1d 23h 987ms โ โโโโโโโโโโโโโโโโโโ
Select all duration columns that have โmsโ OR โnsโ precision:
>>> df.select(cs.duration(["ms", "ns"])) shape: (2, 2) โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ โ td1 โ td3 โ โ --- โ --- โ โ duration[ms] โ duration[ns] โ โโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโโโโโโโโโก โ 1d 2m 3s 456ms โ 18d 999999ยตs โ โ 1d 23h 987ms โ 21d 2m 3s 456001ยตs โ โโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโ
Select all columns except for duration columns:
>>> df.select(~cs.duration()) shape: (2, 1) โโโโโโโโโโโโโโ โ dt โ โ --- โ โ date โ โโโโโโโโโโโโโโก โ 2022-01-31 โ โ 2025-07-05 โ โโโโโโโโโโโโโโ
- polars.selectors.ends_with(*suffix: str) Selector[source]#
Select columns that end with the given substring(s).
- Parameters:
- suffix
Substring(s) that matching column names should end with.
See also
containsSelect columns that contain the given literal substring(s).
matchesSelect all columns that match the given regex pattern.
starts_withSelect columns that start with the given substring(s).
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [False, True], ... } ... )
Select columns that end with the substring โzโ:
>>> df.select(cs.ends_with("z")) shape: (2, 1) โโโโโโโ โ baz โ โ --- โ โ f64 โ โโโโโโโก โ 2.0 โ โ 5.5 โ โโโโโโโ
Select columns that end with either the letter โzโ or โrโ:
>>> df.select(cs.ends_with("z", "r")) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
Select all columns except for those that end with the substring โzโ:
>>> df.select(~cs.ends_with("z")) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโโโ โ foo โ bar โ zap โ โ --- โ --- โ --- โ โ str โ i64 โ bool โ โโโโโโโชโโโโโโชโโโโโโโโก โ x โ 123 โ false โ โ y โ 456 โ true โ โโโโโโโดโโโโโโดโโโโโโโโ
- polars.selectors.enum() Selector[source]#
Select all enum columns.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
See also
by_dtypeSelect all columns matching the given dtype(s).
categoricalSelect all categorical columns.
stringSelect all string columns (optionally including categoricals).
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["xx", "yy"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... }, ... schema_overrides={"foo": pl.Enum(["xx", "yy"])}, ... )
Select all enum columns:
>>> df.select(cs.enum()) shape: (2, 1) โโโโโโโโ โ foo โ โ --- โ โ enum โ โโโโโโโโก โ xx โ โ yy โ โโโโโโโโ
Select all columns except for those that are enum:
>>> df.select(~cs.enum()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
- polars.selectors.exclude(
- columns: str | PolarsDataType | Selector | Expr | Collection[str | PolarsDataType | Selector | Expr],
- *more_columns: str | PolarsDataType | Selector | Expr,
Select all columns except those matching the given columns, datatypes, or selectors.
- Parameters:
- columns
One or more columns (col or name), datatypes, columns, or selectors representing the columns to exclude.
- *more_columns
Additional columns, datatypes, or selectors to exclude, specified as positional arguments.
Notes
If excluding a single selector it is simpler to write as
~selectorinstead.Examples
Exclude by column name(s):
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "aa": [1, 2, 3], ... "ba": ["a", "b", None], ... "cc": [None, 2.5, 1.5], ... } ... ) >>> df.select(cs.exclude("ba", "xx")) shape: (3, 2) โโโโโโโฌโโโโโโโ โ aa โ cc โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโโก โ 1 โ null โ โ 2 โ 2.5 โ โ 3 โ 1.5 โ โโโโโโโดโโโโโโโ
Exclude using a column name, a selector, and a dtype:
>>> df.select(cs.exclude("aa", cs.string(), pl.UInt32)) shape: (3, 1) โโโโโโโโ โ cc โ โ --- โ โ f64 โ โโโโโโโโก โ null โ โ 2.5 โ โ 1.5 โ โโโโโโโโ
- polars.selectors.expand_selector(
- target: DataFrame | LazyFrame | Mapping[str, PolarsDataType],
- selector: Selector | Expr,
- *,
- strict: bool = True,
Expand selector to column names, with respect to a specific frame or target schema.
Added in version 0.20.30: The
strictparameter was added.- Parameters:
- target
A Polars DataFrame, LazyFrame or Schema.
- selector
An arbitrary polars selector (or compound selector).
- strict
Setting False additionally allows for a broader range of column selection expressions (such as bare columns or use of
.exclude()) to be expanded, not just the dedicated selectors.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "colx": ["a", "b", "c"], ... "coly": [123, 456, 789], ... "colz": [2.0, 5.5, 8.0], ... } ... )
Expand selector with respect to an existing
DataFrame:>>> cs.expand_selector(df, cs.numeric()) ('coly', 'colz') >>> cs.expand_selector(df, cs.first() | cs.last()) ('colx', 'colz')
This also works with
LazyFrame:>>> cs.expand_selector(df.lazy(), ~(cs.first() | cs.last())) ('coly',)
Expand selector with respect to a standalone
Schemadict:>>> schema = { ... "id": pl.Int64, ... "desc": pl.String, ... "count": pl.UInt32, ... "value": pl.Float64, ... } >>> cs.expand_selector(schema, cs.string() | cs.float()) ('desc', 'value')
Allow for non-strict selection expressions (such as those including use of an
.exclude()constraint) to be expanded:>>> cs.expand_selector(schema, cs.numeric().exclude("id"), strict=False) ('count', 'value')
- polars.selectors.first(*, strict: bool = True) Selector[source]#
Select the first column in the current scope.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [0, 1], ... } ... )
Select the first column:
>>> df.select(cs.first()) shape: (2, 1) โโโโโโโ โ foo โ โ --- โ โ str โ โโโโโโโก โ x โ โ y โ โโโโโโโ
Select everything except for the first column:
>>> df.select(~cs.first()) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโ โ bar โ baz โ zap โ โ --- โ --- โ --- โ โ i64 โ f64 โ i64 โ โโโโโโโชโโโโโโชโโโโโโก โ 123 โ 2.0 โ 0 โ โ 456 โ 5.5 โ 1 โ โโโโโโโดโโโโโโดโโโโโโ
- polars.selectors.float() Selector[source]#
Select all float columns.
See also
integerSelect all integer columns.
numericSelect all numeric columns.
signed_integerSelect all signed integer columns.
unsigned_integerSelect all unsigned integer columns.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [0.0, 1.0], ... }, ... schema_overrides={"baz": pl.Float32, "zap": pl.Float64}, ... )
Select all float columns:
>>> df.select(cs.float()) shape: (2, 2) โโโโโโโฌโโโโโโ โ baz โ zap โ โ --- โ --- โ โ f32 โ f64 โ โโโโโโโชโโโโโโก โ 2.0 โ 0.0 โ โ 5.5 โ 1.0 โ โโโโโโโดโโโโโโ
Select all columns except for those that are float:
>>> df.select(~cs.float()) shape: (2, 2) โโโโโโโฌโโโโโโ โ foo โ bar โ โ --- โ --- โ โ str โ i64 โ โโโโโโโชโโโโโโก โ x โ 123 โ โ y โ 456 โ โโโโโโโดโโโโโโ
- polars.selectors.integer() Selector[source]#
Select all integer columns.
See also
by_dtypeSelect columns by dtype.
floatSelect all float columns.
numericSelect all numeric columns.
signed_integerSelect all signed integer columns.
unsigned_integerSelect all unsigned integer columns.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [0, 1], ... } ... )
Select all integer columns:
>>> df.select(cs.integer()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ zap โ โ --- โ --- โ โ i64 โ i64 โ โโโโโโโชโโโโโโก โ 123 โ 0 โ โ 456 โ 1 โ โโโโโโโดโโโโโโ
Select all columns except for those that are integer :
>>> df.select(~cs.integer()) shape: (2, 2) โโโโโโโฌโโโโโโ โ foo โ baz โ โ --- โ --- โ โ str โ f64 โ โโโโโโโชโโโโโโก โ x โ 2.0 โ โ y โ 5.5 โ โโโโโโโดโโโโโโ
- polars.selectors.is_selector(obj: Any) TypeIs[Selector][source]#
Indicate whether the given object/expression is a selector.
Examples
>>> from polars.selectors import is_selector >>> import polars.selectors as cs >>> is_selector(pl.col("colx")) False >>> is_selector(cs.first() | cs.last()) True
- polars.selectors.last(*, strict: bool = True) Selector[source]#
Select the last column in the current scope.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [0, 1], ... } ... )
Select the last column:
>>> df.select(cs.last()) shape: (2, 1) โโโโโโโ โ zap โ โ --- โ โ i64 โ โโโโโโโก โ 0 โ โ 1 โ โโโโโโโ
Select everything except for the last column:
>>> df.select(~cs.last()) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโ โ foo โ bar โ baz โ โ --- โ --- โ --- โ โ str โ i64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโก โ x โ 123 โ 2.0 โ โ y โ 456 โ 5.5 โ โโโโโโโดโโโโโโดโโโโโโ
- polars.selectors.list(inner: None | Selector = None) Selector[source]#
Select all list columns.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [["xx", "yy"], ["x"]], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... }, ... )
Select all list columns:
>>> df.select(cs.list()) shape: (2, 1) โโโโโโโโโโโโโโโโ โ foo โ โ --- โ โ list[str] โ โโโโโโโโโโโโโโโโก โ ["xx", "yy"] โ โ ["x"] โ โโโโโโโโโโโโโโโโ
Select all columns except for those that are list:
>>> df.select(~cs.list()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
Select all list columns with a certain matching inner type:
>>> df.select(cs.list(cs.string())) shape: (2, 1) โโโโโโโโโโโโโโโโ โ foo โ โ --- โ โ list[str] โ โโโโโโโโโโโโโโโโก โ ["xx", "yy"] โ โ ["x"] โ โโโโโโโโโโโโโโโโ >>> df.select(cs.list(cs.integer())) shape: (0, 0) โโ โโก โโ
- polars.selectors.map() Selector[source]#
Select all map columns.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": pl.Series( ... [{"a": 1}, {"b": 2}], dtype=pl.Map(pl.String, pl.Int64) ... ), ... "bar": [123, 456], ... }, ... )
Select all map columns:
>>> df.select(cs.map()) shape: (2, 1) โโโโโโโโโโโโโโโโโ โ foo โ โ --- โ โ map[str, i64] โ โโโโโโโโโโโโโโโโโก โ {"a": 1} โ โ {"b": 2} โ โโโโโโโโโโโโโโโโโ
Select all columns except for those that are maps:
>>> df.select(~cs.map()) shape: (2, 1) โโโโโโโ โ bar โ โ --- โ โ i64 โ โโโโโโโก โ 123 โ โ 456 โ โโโโโโโ
- polars.selectors.matches(pattern: str) Selector[source]#
Select all columns that match the given regex pattern.
- Parameters:
- pattern
A valid regular expression pattern, compatible with the regex crate.
See also
containsSelect all columns that contain the given substring.
ends_withSelect all columns that end with the given substring(s).
starts_withSelect all columns that start with the given substring(s).
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [0, 1], ... } ... )
Match column names containing an โaโ, preceded by a character that is not โzโ:
>>> df.select(cs.matches("[^z]a")) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
Do not match column names ending in โRโ or โzโ (case-insensitively):
>>> df.select(~cs.matches(r"(?i)R|z$")) shape: (2, 2) โโโโโโโฌโโโโโโ โ foo โ zap โ โ --- โ --- โ โ str โ i64 โ โโโโโโโชโโโโโโก โ x โ 0 โ โ y โ 1 โ โโโโโโโดโโโโโโ
- polars.selectors.nested() Selector[source]#
Select all nested columns.
A nested column is a list, array or struct.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [{"a": "xx", "b": "z"}, {"a": "x", "b": "y"}], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "wow": [[1, 2], [3]], ... }, ... )
Select all nested columns:
>>> df.select(cs.nested()) shape: (2, 2) โโโโโโโโโโโโโโฌโโโโโโโโโโโโ โ foo โ wow โ โ --- โ --- โ โ struct[2] โ list[i64] โ โโโโโโโโโโโโโโชโโโโโโโโโโโโก โ {"xx","z"} โ [1, 2] โ โ {"x","y"} โ [3] โ โโโโโโโโโโโโโโดโโโโโโโโโโโโ
Select all columns except for those that are nested:
>>> df.select(~cs.nested()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
- polars.selectors.numeric() Selector[source]#
Select all numeric columns.
See also
by_dtypeSelect columns by dtype.
floatSelect all float columns.
integerSelect all integer columns.
signed_integerSelect all signed integer columns.
unsigned_integerSelect all unsigned integer columns.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": ["x", "y"], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... "zap": [0, 0], ... }, ... schema_overrides={"bar": pl.Int16, "baz": pl.Float32, "zap": pl.UInt8}, ... )
Match all numeric columns:
>>> df.select(cs.numeric()) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโ โ bar โ baz โ zap โ โ --- โ --- โ --- โ โ i16 โ f32 โ u8 โ โโโโโโโชโโโโโโชโโโโโโก โ 123 โ 2.0 โ 0 โ โ 456 โ 5.5 โ 0 โ โโโโโโโดโโโโโโดโโโโโโ
Match all columns except for those that are numeric:
>>> df.select(~cs.numeric()) shape: (2, 1) โโโโโโโ โ foo โ โ --- โ โ str โ โโโโโโโก โ x โ โ y โ โโโโโโโ
- polars.selectors.signed_integer() Selector[source]#
Select all signed integer columns.
See also
by_dtypeSelect columns by dtype.
floatSelect all float columns.
integerSelect all integer columns.
numericSelect all numeric columns.
unsigned_integerSelect all unsigned integer columns.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [-123, -456], ... "bar": [3456, 6789], ... "baz": [7654, 4321], ... "zap": ["ab", "cd"], ... }, ... schema_overrides={"bar": pl.UInt32, "baz": pl.UInt64}, ... )
Select all signed integer columns:
>>> df.select(cs.signed_integer()) shape: (2, 1) โโโโโโโโ โ foo โ โ --- โ โ i64 โ โโโโโโโโก โ -123 โ โ -456 โ โโโโโโโโ
>>> df.select(~cs.signed_integer()) shape: (2, 3) โโโโโโโโฌโโโโโโโฌโโโโโโ โ bar โ baz โ zap โ โ --- โ --- โ --- โ โ u32 โ u64 โ str โ โโโโโโโโชโโโโโโโชโโโโโโก โ 3456 โ 7654 โ ab โ โ 6789 โ 4321 โ cd โ โโโโโโโโดโโโโโโโดโโโโโโ
Select all integer columns (both signed and unsigned):
>>> df.select(cs.integer()) shape: (2, 3) โโโโโโโโฌโโโโโโโฌโโโโโโโ โ foo โ bar โ baz โ โ --- โ --- โ --- โ โ i64 โ u32 โ u64 โ โโโโโโโโชโโโโโโโชโโโโโโโก โ -123 โ 3456 โ 7654 โ โ -456 โ 6789 โ 4321 โ โโโโโโโโดโโโโโโโดโโโโโโโ
- polars.selectors.starts_with(*prefix: str) Selector[source]#
Select columns that start with the given substring(s).
- Parameters:
- prefix
Substring(s) that matching column names should start with.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [1.0, 2.0], ... "bar": [3.0, 4.0], ... "baz": [5, 6], ... "zap": [7, 8], ... } ... )
Match columns starting with a โbโ:
>>> df.select(cs.starts_with("b")) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ f64 โ i64 โ โโโโโโโชโโโโโโก โ 3.0 โ 5 โ โ 4.0 โ 6 โ โโโโโโโดโโโโโโ
Match columns starting with either the letter โbโ or โzโ:
>>> df.select(cs.starts_with("b", "z")) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโ โ bar โ baz โ zap โ โ --- โ --- โ --- โ โ f64 โ i64 โ i64 โ โโโโโโโชโโโโโโชโโโโโโก โ 3.0 โ 5 โ 7 โ โ 4.0 โ 6 โ 8 โ โโโโโโโดโโโโโโดโโโโโโ
Match all columns except for those starting with โbโ:
>>> df.select(~cs.starts_with("b")) shape: (2, 2) โโโโโโโฌโโโโโโ โ foo โ zap โ โ --- โ --- โ โ f64 โ i64 โ โโโโโโโชโโโโโโก โ 1.0 โ 7 โ โ 2.0 โ 8 โ โโโโโโโดโโโโโโ
- polars.selectors.string(*, include_categorical: bool = False) Selector[source]#
Select all String (and, optionally, Categorical) string columns.
See also
binarySelect all binary columns.
by_dtypeSelect all columns matching the given dtype(s).
categoricalSelect all categorical columns.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "w": ["xx", "yy", "xx", "yy", "xx"], ... "x": [1, 2, 1, 4, -2], ... "y": [3.0, 4.5, 1.0, 2.5, -2.0], ... "z": ["a", "b", "a", "b", "b"], ... }, ... ).with_columns( ... z=pl.col("z").cast(pl.Categorical()), ... )
Group by all string columns, sum the numeric columns, then sort by the string cols:
>>> df.group_by(cs.string()).agg(cs.numeric().sum()).sort(by=cs.string()) shape: (2, 3) โโโโโโโฌโโโโโโฌโโโโโโ โ w โ x โ y โ โ --- โ --- โ --- โ โ str โ i64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโก โ xx โ 0 โ 2.0 โ โ yy โ 6 โ 7.0 โ โโโโโโโดโโโโโโดโโโโโโ
Group by all string and categorical columns:
>>> df.group_by(cs.string(include_categorical=True)).agg(cs.numeric().sum()).sort( ... by=cs.string(include_categorical=True) ... ) shape: (3, 4) โโโโโโโฌโโโโโโฌโโโโโโฌโโโโโโโ โ w โ z โ x โ y โ โ --- โ --- โ --- โ --- โ โ str โ cat โ i64 โ f64 โ โโโโโโโชโโโโโโชโโโโโโชโโโโโโโก โ xx โ a โ 2 โ 4.0 โ โ xx โ b โ -2 โ -2.0 โ โ yy โ b โ 6 โ 7.0 โ โโโโโโโดโโโโโโดโโโโโโดโโโโโโโ
- polars.selectors.struct() Selector[source]#
Select all struct columns.
Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
See also
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [{"a": "xx", "b": "z"}, {"a": "x", "b": "y"}], ... "bar": [123, 456], ... "baz": [2.0, 5.5], ... }, ... )
Select all struct columns:
>>> df.select(cs.struct()) shape: (2, 1) โโโโโโโโโโโโโโ โ foo โ โ --- โ โ struct[2] โ โโโโโโโโโโโโโโก โ {"xx","z"} โ โ {"x","y"} โ โโโโโโโโโโโโโโ
Select all columns except for those that are struct:
>>> df.select(~cs.struct()) shape: (2, 2) โโโโโโโฌโโโโโโ โ bar โ baz โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโชโโโโโโก โ 123 โ 2.0 โ โ 456 โ 5.5 โ โโโโโโโดโโโโโโ
- polars.selectors.temporal() Selector[source]#
Select all temporal columns.
See also
Examples
>>> from datetime import date, time >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "dt": [date(2021, 1, 1), date(2021, 1, 2)], ... "tm": [time(12, 0, 0), time(20, 30, 45)], ... "value": [1.2345, 2.3456], ... } ... )
Match all temporal columns:
>>> df.select(cs.temporal()) shape: (2, 2) โโโโโโโโโโโโโโฌโโโโโโโโโโโ โ dt โ tm โ โ --- โ --- โ โ date โ time โ โโโโโโโโโโโโโโชโโโโโโโโโโโก โ 2021-01-01 โ 12:00:00 โ โ 2021-01-02 โ 20:30:45 โ โโโโโโโโโโโโโโดโโโโโโโโโโโ
Match all temporal columns except for time columns:
>>> df.select(cs.temporal() - cs.time()) shape: (2, 1) โโโโโโโโโโโโโโ โ dt โ โ --- โ โ date โ โโโโโโโโโโโโโโก โ 2021-01-01 โ โ 2021-01-02 โ โโโโโโโโโโโโโโ
Match all columns except for temporal columns:
>>> df.select(~cs.temporal()) shape: (2, 1) โโโโโโโโโโ โ value โ โ --- โ โ f64 โ โโโโโโโโโโก โ 1.2345 โ โ 2.3456 โ โโโโโโโโโโ
- polars.selectors.time() Selector[source]#
Select all time columns.
See also
Examples
>>> from datetime import date, datetime, time >>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "dtm": [datetime(2001, 5, 7, 10, 25), datetime(2031, 12, 31, 0, 30)], ... "dt": [date(1999, 12, 31), date(2024, 8, 9)], ... "tm": [time(0, 0, 0), time(23, 59, 59)], ... }, ... )
Select all time columns:
>>> df.select(cs.time()) shape: (2, 1) โโโโโโโโโโโโ โ tm โ โ --- โ โ time โ โโโโโโโโโโโโก โ 00:00:00 โ โ 23:59:59 โ โโโโโโโโโโโโ
Select all columns except for those that are times:
>>> df.select(~cs.time()) shape: (2, 2) โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโ โ dtm โ dt โ โ --- โ --- โ โ datetime[ฮผs] โ date โ โโโโโโโโโโโโโโโโโโโโโโโชโโโโโโโโโโโโโก โ 2001-05-07 10:25:00 โ 1999-12-31 โ โ 2031-12-31 00:30:00 โ 2024-08-09 โ โโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโ
- polars.selectors.unsigned_integer() Selector[source]#
Select all unsigned integer columns.
See also
by_dtypeSelect columns by dtype.
floatSelect all float columns.
integerSelect all integer columns.
numericSelect all numeric columns.
signed_integerSelect all signed integer columns.
Examples
>>> import polars.selectors as cs >>> df = pl.DataFrame( ... { ... "foo": [-123, -456], ... "bar": [3456, 6789], ... "baz": [7654, 4321], ... "zap": ["ab", "cd"], ... }, ... schema_overrides={"bar": pl.UInt32, "baz": pl.UInt64}, ... )
Select all unsigned integer columns:
>>> df.select(cs.unsigned_integer()) shape: (2, 2) โโโโโโโโฌโโโโโโโ โ bar โ baz โ โ --- โ --- โ โ u32 โ u64 โ โโโโโโโโชโโโโโโโก โ 3456 โ 7654 โ โ 6789 โ 4321 โ โโโโโโโโดโโโโโโโ
Select all columns except for those that are unsigned integers:
>>> df.select(~cs.unsigned_integer()) shape: (2, 2) โโโโโโโโฌโโโโโโ โ foo โ zap โ โ --- โ --- โ โ i64 โ str โ โโโโโโโโชโโโโโโก โ -123 โ ab โ โ -456 โ cd โ โโโโโโโโดโโโโโโ
Select all integer columns (both signed and unsigned):
>>> df.select(cs.integer()) shape: (2, 3) โโโโโโโโฌโโโโโโโฌโโโโโโโ โ foo โ bar โ baz โ โ --- โ --- โ --- โ โ i64 โ u32 โ u64 โ โโโโโโโโชโโโโโโโชโโโโโโโก โ -123 โ 3456 โ 7654 โ โ -456 โ 6789 โ 4321 โ โโโโโโโโดโโโโโโโดโโโโโโโ