{"record":{"id":"18a65b57aae50601","repo":"pola-rs/polars","slug":"from-cats-and-dtype-called-on-non-categorical-type","errorCode":null,"errorMessage":"from_cats_and_dtype called on non-categorical type","messagePattern":"from_cats_and_dtype called on non-categorical type","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/chunked_array/logical/categorical.rs","lineNumber":80,"sourceCode":"    pub fn uses_lexical_ordering(&self) -> bool {\n        !self.is_enum()\n    }\n\n    pub fn full_null_with_dtype(name: PlSmallStr, length: usize, dtype: DataType) -> Self {\n        let phys =\n            ChunkedArray::<<T as PolarsCategoricalType>::PolarsPhysical>::full_null(name, length);\n        unsafe { Self::from_cats_and_dtype_unchecked(phys, dtype) }\n    }\n\n    /// Create a [`CategoricalChunked`] from a physical array and dtype.\n    ///\n    /// Checks that all the category ids are valid, mapping invalid ones to nulls.\n    pub fn from_cats_and_dtype(\n        mut cat_ids: ChunkedArray<T::PolarsPhysical>,\n        dtype: DataType,\n    ) -> Self {\n        let (DataType::Enum(_, mapping) | DataType::Categorical(_, mapping)) = &dtype else {\n            panic!(\"from_cats_and_dtype called on non-categorical type\")\n        };\n        assert!(dtype.cat_physical().ok() == Some(T::physical()));\n\n        unsafe {\n            let mut invariants_violated = false;\n            let mut validity = BitmapBuilder::new();\n            for arr in cat_ids.downcast_iter_mut() {\n                validity.reserve(arr.len());\n                if arr.has_nulls() {\n                    for opt_cat_id in arr.iter() {\n                        if let Some(cat_id) = opt_cat_id {\n                            validity.push_unchecked(mapping.cat_to_str(cat_id.as_cat()).is_some());\n                        } else {\n                            validity.push_unchecked(false);\n                        }\n                    }\n                } else {\n                    for cat_id in arr.values_iter() {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/pola-rs/polars/blob/5d8ebabf11caea54a5c29178a64a058762f49766/crates/polars-core/src/chunked_array/logical/categorical.rs#L62-L98","documentation":"Type-safety assertion inside from_cats_and_dtype: the supplied `dtype` is not a Categorical or Enum DataType, so constructing a CategoricalChunked from physical category ids against it is invalid. This is an internal/API-misuse guard — callers must pass a categorical-family dtype.","triggerScenarios":"This panic/expect fires when execution reaches an unguarded state described by: \"from_cats_and_dtype called on non-categorical type\". Typical triggers: unsupported dtype or feature-gated code path reached at runtime, invalid user input or environment variable value, calling API methods in the wrong order or on mismatched types, or data (lengths, offsets, ranges) violating the function's preconditions.","commonSituations":"Encountered when input data or configuration does not meet the preconditions of the throwing code path (\"from_cats_and_dtype called on non-categorical type\"). Common cases: missing Cargo feature flags, mismatched dtypes/lengths between arrays or series, out-of-range temporal values, malformed environment variables, or operations on unsupported/complex nested types.","solutions":["Call `from_cats_and_dtype` only on a Categorical/Enum chunked array; check the dtype before calling.","Cast the series to Categorical first if that is the intended type."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5d8ebabf11caea54a5c29178a64a058762f49766","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}