{"record":{"id":"60417628434af9fa","repo":"pola-rs/polars","slug":"can-t-order-enums-from-different-frozencategories","errorCode":null,"errorMessage":"can't order enums from different FrozenCategories","messagePattern":"can't order enums from different FrozenCategories","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":1453,"sourceCode":"            (Duration(lt, lu), Duration(rt, ru)) => {\n                if lu != ru {\n                    unimplemented!(\"comparing durations with different units is not supported\");\n                }\n\n                lt.partial_cmp(rt)\n            },\n            #[cfg(feature = \"dtype-time\")]\n            (Time(l), Time(r)) => l.partial_cmp(r),\n            #[cfg(feature = \"dtype-categorical\")]\n            (Categorical(l_cat, l_map), Categorical(r_cat, r_map)) => unsafe {\n                let l_str = l_map.cat_to_str_unchecked(*l_cat);\n                let r_str = r_map.cat_to_str_unchecked(*r_cat);\n                l_str.partial_cmp(r_str)\n            },\n            #[cfg(feature = \"dtype-categorical\")]\n            (Enum(l_cat, l_map), Enum(r_cat, r_map)) => {\n                if !Arc::ptr_eq(l_map, r_map) {\n                    unimplemented!(\"can't order enums from different FrozenCategories\")\n                }\n                l_cat.partial_cmp(r_cat)\n            },\n            (List(_), List(_)) => {\n                unimplemented!(\"ordering for List dtype is not supported\")\n            },\n            #[cfg(feature = \"dtype-map\")]\n            (Map(_), Map(_)) => {\n                unimplemented!(\"ordering for Map dtype is not supported\")\n            },\n            #[cfg(feature = \"dtype-array\")]\n            (Array(..), Array(..)) => {\n                unimplemented!(\"ordering for Array dtype is not supported\")\n            },\n            #[cfg(feature = \"object\")]\n            (Object(_), Object(_)) => {\n                unimplemented!(\"ordering for Object dtype is not supported\")\n            },","sourceCodeStart":1435,"sourceCodeEnd":1471,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L1435-L1471","documentation":"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.)","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the same DataType::Enum (same shared FrozenCategories) for both sides so indices are comparable","Cast both Enum columns to Str and compare/sort the string values","Cast to physical u32 only after verifying both mappings contain the same categories in the same order","Use expression-level comparisons on a single DataFrame where the Enum dtype is unified"],"exampleFix":"// before: ordering Enum values from two different FrozenCategories\nlet e1 = s1.cast(&DataType::Enum(None, Arc::new(PlSmallStr::from_static(\"Enum1\")))).unwrap();\nlet e2 = s2.cast(&DataType::Enum(None, Arc::new(PlSmallStr::from_static(\"Enum2\")))).unwrap();\n// AnyValue::partial_cmp on their values panics\n// after: compare as strings\nlet e1 = s1.cast(&DataType::String).unwrap();\nlet e2 = s2.cast(&DataType::String).unwrap();\nlet _ = e1.equal(&e2)?;","handlingStrategy":"validation","validationCode":"fn enums_orderable(a: &AnyValue, b: &AnyValue) -> bool {\n    match (a, b) {\n        (AnyValue::Enum(_, l), AnyValue::Enum(_, r)) => Arc::ptr_eq(l, r),\n        _ => false,\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Share one FrozenCategories/Enum definition across all compared columns","Sort/compar on Str casts when enum provenance is uncertain","Do not rebuild Enum dtypes per-frame; reuse the dtype from the schema","Verify Arc::ptr_eq (or dtype equality) before ordering enum values"],"tags":["polars","enum","ordering","unsupported-operation"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}