pola-rs/polars · error

ordering for Map dtype is not supported

Error message

ordering for Map dtype is not supported

What it means

AnyValue::partial_cmp does not define an ordering for Map-typed scalar values and panics via unimplemented!(). Polars treats maps as unordered key-value collections, so comparing two Map values for sort/comparison purposes is intentionally unsupported.

Solutions

  1. Sort by a specific key: pl.col('m').struct.field('key') or extract the relevant value first
  2. Convert the map to a struct or list and order by one of its fields
  3. Drop the map column from the sort keys
  4. Compute ordering in Python via map_elements if performance allows

Example fix

// before
df.sort('props')  # props: Map -> panics
// after
df.sort(pl.col('props').struct.field('score'))
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(df.schema[col], (pl.Struct,)) or str(df.schema[col]).startswith('Map'):
    raise ValueError(f'{col} is not scalar-orderable; pick a key to sort by')

Type guard

def is_map_dtype(dtype) -> bool:
    return 'Map' in str(dtype)

Prevention

When it happens

Trigger: Sorting or comparing a column of dtype Map, e.g. df.sort on a map column, or any operation that invokes AnyValue::partial_cmp on Map scalars (feature 'dtype-map' enabled).

Common situations: Users with map data (often from other engines) trying to sort a DataFrame by a map column; using min/max on a map column.

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/aafbd2e7dbedbeb8. Report an issue: GitHub.

Appendix: source

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

            #[cfg(feature = "dtype-categorical")]
            (Categorical(l_cat, l_map), Categorical(r_cat, r_map)) => unsafe {
                let l_str = l_map.cat_to_str_unchecked(*l_cat);
                let r_str = r_map.cat_to_str_unchecked(*r_cat);
                l_str.partial_cmp(r_str)
            },
            #[cfg(feature = "dtype-categorical")]
            (Enum(l_cat, l_map), Enum(r_cat, r_map)) => {
                if !Arc::ptr_eq(l_map, r_map) {
                    unimplemented!("can't order enums from different FrozenCategories")
                }
                l_cat.partial_cmp(r_cat)
            },
            (List(_), List(_)) => {
                unimplemented!("ordering for List dtype is not supported")
            },
            #[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)),

View on GitHub (pinned to fe841f959e)