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 has no ordering for Struct scalars (all Struct/StructOwned combinations). Ordering nested structs field-by-field is not implemented at the scalar level, so any comparison attempt panics.

Source

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

                l_cat.partial_cmp(r_cat)
            },
            (List(_), List(_)) => {
                unimplemented!("ordering for List 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 df599052da)

Solutions

  1. Order by extracted struct fields: df.sort(pl.col("s").struct.field("priority"))
  2. Flatten the struct (df.unnest("s")) and sort by the resulting columns
  3. Compute a comparison key from the struct fields and sort by that instead of comparing whole structs

Example fix

# before
vals = sorted(df["s"].to_list())  # struct scalars -> panic on comparison

# after
vals = df.sort(pl.col("s").struct.field("priority"))["s"].to_list()
Defensive patterns

Strategy: type-guard

Validate before calling

def orderable_dtype(dt) -> bool:
    return not isinstance(dt, (pl.List, pl.Array, pl.Struct)) and dt != pl.Object

Type guard

def orderable_dtype(dt) -> bool:
    return not isinstance(dt, (pl.List, pl.Array, pl.Struct)) and dt != pl.Object

Prevention

When it happens

Trigger: Comparing struct cell values: df["s"].get(0) < df["s"].get(1), sorted() over values from a Struct column, or scalar min/max over struct fields.

Common situations: Rows carrying struct payloads (JSON-derived data, named tuples) where user code tries to rank cells or sort raw Python/polars struct scalars directly.

Related errors


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