Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polars selectors for columns that are Nested Types

Some Polars operations, such as .sort() fail when passed a column with a Nested Type.

This (sensible) choice about sort means I cannot use my usual sorting pattern of df.sort(pl.all()).

import polars as pl

NESTED_TYPES = [
  pl.List,
  pl.Array,
  pl.Object,
  pl.Struct
]
pl.exclude(NESTED_TYPES)

Result:

*.exclude([Dtype(List(Null)), Dtype(Array(Null, 0)), Dtype(Object("object", None)), Dtype(Struct([]))])

Is there a way to select (or exclude) only nested types?

The Selectors Documentation has many ideas but nothing seems right for this.

like image 587
Gregg Lind Avatar asked Dec 09 '25 16:12

Gregg Lind


1 Answers

It is not very well supported yet, see https://github.com/pola-rs/polars/issues/9971

As a workaround, you can specify "all types except non-nested types" using the code shown in that Issue, or just loop over all dtypes you care about and specify pl.List(dtype) for each of them, but there aren't any good ways to say "all list dtypes" yet.

like image 62
etrotta Avatar answered Dec 11 '25 06:12

etrotta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!