{"record":{"id":"73c60a67a709a81f","repo":"pola-rs/polars","slug":"can-not-get-dtype-of-enum-anyvalue","errorCode":null,"errorMessage":"can not get dtype of Enum AnyValue","messagePattern":"can not get dtype of Enum AnyValue","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":255,"sourceCode":"            Binary(_) | BinaryOwned(_) => DataType::Binary,\n            #[cfg(feature = \"dtype-date\")]\n            Date(_) => DataType::Date,\n            #[cfg(feature = \"dtype-time\")]\n            Time(_) => DataType::Time,\n            #[cfg(feature = \"dtype-datetime\")]\n            Datetime(_, tu, tz) => DataType::Datetime(*tu, (*tz).cloned()),\n            #[cfg(feature = \"dtype-datetime\")]\n            DatetimeOwned(_, tu, tz) => {\n                DataType::Datetime(*tu, tz.as_ref().map(|v| v.as_ref().clone()))\n            },\n            #[cfg(feature = \"dtype-duration\")]\n            Duration(_, tu) => DataType::Duration(*tu),\n            #[cfg(feature = \"dtype-categorical\")]\n            Categorical(_, _) | CategoricalOwned(_, _) => {\n                unimplemented!(\"can not get dtype of Categorical AnyValue\")\n            },\n            #[cfg(feature = \"dtype-categorical\")]\n            Enum(_, _) | EnumOwned(_, _) => unimplemented!(\"can not get dtype of Enum AnyValue\"),\n            List(s) => DataType::List(Box::new(s.dtype().clone())),\n            #[cfg(feature = \"dtype-map\")]\n            Map(s) => {\n                let DataType::Struct(fields) = s.dtype() else {\n                    unimplemented!(\"Map AnyValue payload must be a Struct\")\n                };\n                DataType::Map(\n                    Box::new(fields[0].dtype().clone()),\n                    Box::new(fields[1].dtype().clone()),\n                )\n            },\n            #[cfg(feature = \"dtype-array\")]\n            Array(s, size) => DataType::Array(Box::new(s.dtype().clone()), *size),\n            #[cfg(feature = \"dtype-struct\")]\n            Struct(_, _, fields) => DataType::Struct(fields.to_vec()),\n            #[cfg(feature = \"dtype-struct\")]\n            StructOwned(payload) => DataType::Struct(payload.1.clone()),\n            #[cfg(feature = \"dtype-decimal\")]","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L237-L273","documentation":"Like Categorical, AnyValue::Enum/EnumOwned stores only the physical index and AnyValue::dtype() has no access to the Enum's ordered categories, so reconstructing DataType::Enum is impossible and the code panics with unimplemented!.","triggerScenarios":"Calling .dtype() on an AnyValue::Enum or AnyValue::EnumOwned, e.g. via get()/iter() on an Enum-typed series or via generic code paths that invoke dtype() on any extracted scalar.","commonSituations":"Working with DataType::Enum columns (fixed set of categories) and inspecting single values; generic scalar-processing helpers hitting Enum values at runtime.","solutions":["Read the dtype from the Series (series.dtype()) rather than from the AnyValue.","Cast the series to String/Utf8 before extracting per-value data if you need the category text.","Match Enum variants explicitly in scalar code and consult the Enum's DataType (which you must hold alongside) for category info.","Skip Enum columns in generic scalar inspection code, treating them like Categorical."],"exampleFix":"// before\nlet dtype = av.dtype(); // panics for Enum AnyValues\n// after\nlet dtype = enum_series.dtype(); // DataType::Enum(categories, ordering)","handlingStrategy":"type-guard","validationCode":"fn scalar_dtype(av: &AnyValue) -> Option<DataType> {\n    match av {\n        AnyValue::Enum(..) | AnyValue::EnumOwned(..) => None,\n        _ => Some(av.dtype()),\n    }\n}","typeGuard":"if matches!(av, AnyValue::Enum(..) | AnyValue::EnumOwned(..)) { /* use series dtype */ }","tryCatchPattern":null,"preventionTips":["Don't call dtype() on Enum AnyValues; use the owning Series' dtype","Cast Enum columns to String to read category text per value","Keep the DataType::Enum metadata alongside extracted scalars"],"tags":["polars","panic","enum","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"}