pola-rs/polars · error

ordering for Struct dtype is not supported

Error message

ordering for Struct dtype is not supported

What it means

AnyValue::partial_cmp does not implement ordering for Struct-typed scalars (feature 'dtype-struct') and panics via unimplemented!(). Polars cannot decide an ordering across struct values without a user-specified key column.

Solutions

  1. Sort by an inner field: pl.col('s').struct.field('name')
  2. Use sort_by with a flattened key column
  3. Explode/unpack the struct fields before ordering
  4. Wait for/track upstream struct ordering support

Example fix

// before
df.sort('person')  # person: Struct{name,age} -> panics
// after
df.sort(pl.col('person').struct.field('age'))
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(df.schema[col], pl.Struct):
    key = pl.col(col).struct.field('name')  # pick an inner field

Type guard

def is_struct_dtype(dtype) -> bool:
    return isinstance(dtype, pl.Struct)

Prevention

When it happens

Trigger: Sorting by a Struct column, e.g. df.sort('s') where s: Struct, or any operation that needs scalar comparison of structs via partial_cmp.

Common situations: Nested data ingested from JSON/Arrow producing Struct columns, then attempting sort, min, max, or rank on the struct column directly.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/c9d4e292acb17571. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-core/src/datatypes/any_value.rs:1477

            },
            #[cfg(feature = "dtype-map")]
            (Map(_), Map(_)) => {
                unimplemented!("ordering for Map dtype is not supported")
            },
            #[cfg(feature = "dtype-array")]
            (Array(..), Array(..)) => {
                unimplemented!("ordering for Array dtype is not supported")
            },
            #[cfg(feature = "object")]
            (Object(_), Object(_)) => {
                unimplemented!("ordering for Object dtype is not supported")
            },
            #[cfg(feature = "dtype-struct")]
            (StructOwned(_), StructOwned(_))
            | (StructOwned(_), Struct(..))
            | (Struct(..), StructOwned(_))
            | (Struct(..), Struct(..)) => {
                unimplemented!("ordering for Struct dtype is not supported")
            },
            #[cfg(feature = "dtype-decimal")]
            (Decimal(lv, _lp, ls), Decimal(rv, _rp, rs)) => Some(dec128_cmp(*lv, *ls, *rv, *rs)),

            (_, _) => {
                unimplemented!(
                    "scalar ordering for mixed dtypes {self:?} and {other:?} is not supported"
                )
            },
        }
    }
}

impl TotalEq for AnyValue<'_> {
    #[inline]
    fn tot_eq(&self, other: &Self) -> bool {
        self.eq_missing(other, true)
    }

View on GitHub (pinned to fe841f959e)