pola-rs/polars · error

can't order enums from different FrozenCategories

Error message

can't order enums from different FrozenCategories

What it means

AnyValue::partial_cmp orders Enum values by their physical u32 index, which is only meaningful within one FrozenCategories mapping. When the two Enum AnyValues come from different FrozenCategories Arcs, the indices are incomparable, so the library panics with unimplemented!() instead of returning a wrong ordering. (Unlike Categorical, it does not fall back to string comparison here.)

Solutions

  1. Use the same DataType::Enum (same shared FrozenCategories) for both sides so indices are comparable
  2. Cast both Enum columns to Str and compare/sort the string values
  3. Cast to physical u32 only after verifying both mappings contain the same categories in the same order
  4. Use expression-level comparisons on a single DataFrame where the Enum dtype is unified

Example fix

// before: ordering Enum values from two different FrozenCategories
let e1 = s1.cast(&DataType::Enum(None, Arc::new(PlSmallStr::from_static("Enum1")))).unwrap();
let e2 = s2.cast(&DataType::Enum(None, Arc::new(PlSmallStr::from_static("Enum2")))).unwrap();
// AnyValue::partial_cmp on their values panics
// after: compare as strings
let e1 = s1.cast(&DataType::String).unwrap();
let e2 = s2.cast(&DataType::String).unwrap();
let _ = e1.equal(&e2)?;
Defensive patterns

Strategy: validation

Validate before calling

fn enums_orderable(a: &AnyValue, b: &AnyValue) -> bool {
    match (a, b) {
        (AnyValue::Enum(_, l), AnyValue::Enum(_, r)) => Arc::ptr_eq(l, r),
        _ => false,
    }
}

Prevention

When it happens

Trigger: Calling AnyValue::partial_cmp (e.g. via sort, lt/gt comparisons on extracted values) with two AnyValue::Enum variants whose FrozenRevMapping Arcs are not the same allocation — Enum columns created from different Enums definitions or from different processes.

Common situations: Sorting or comparing Enum values taken from two DataFrames whose Enum types were declared separately, mixing Enum columns after IO round-trips that rebuilt the FrozenCategories, or comparing values against a literal Enum built on the fly with a different mapping.

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

Appendix: source

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

            (Duration(lt, lu), Duration(rt, ru)) => {
                if lu != ru {
                    unimplemented!("comparing durations with different units is not supported");
                }

                lt.partial_cmp(rt)
            },
            #[cfg(feature = "dtype-time")]
            (Time(l), Time(r)) => l.partial_cmp(r),
            #[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")
            },

View on GitHub (pinned to fe841f959e)