pola-rs/polars · error
from_strings_and_dtype_strict called on non-categorical type
Error message
from_strings_and_dtype_strict called on non-categorical type
What it means
Type-safety assertion inside from_strings_and_dtype_strict: the target dtype is not a categorical/enum type, so strict string-to-category conversion cannot proceed. An internal guard against calling the constructor with a non-categorical dtype.
Source
Thrown at crates/polars-core/src/chunked_array/logical/categorical.rs:195
} else {
T::Native::zero()
});
validity.push(opt_s.is_some());
}
},
DataType::Enum(fcats, mapping) => {
assert!(fcats.physical() == T::physical());
for opt_s in strings {
cat_ids.push(if let Some(cat) = opt_s.and_then(|s| mapping.get_cat(s)) {
validity.push(true);
T::Native::from_cat(cat)
} else {
validity.push(false);
T::Native::zero()
});
}
},
_ => panic!("from_strings_and_dtype_strict called on non-categorical type"),
}
let arr = <T::PolarsPhysical as PolarsDataType>::Array::from_vec(cat_ids)
.with_validity(validity.into_opt_validity());
let phys = ChunkedArray::<T::PolarsPhysical>::with_chunk(name, arr);
Ok(unsafe { Self::from_cats_and_dtype_unchecked(phys, dtype) })
}
pub fn to_arrow(&self, compat_level: CompatLevel) -> DictionaryArray<T::Native> {
let keys = self.physical().rechunk();
let keys = keys.downcast_as_array();
let values = self
.get_mapping()
.to_arrow(compat_level.uses_binview_types());
let values_dtype = Box::new(values.dtype().clone());
let dtype = ArrowDataType::Dictionary(
<T::Native as DictionaryKey>::KEY_TYPE,
values_dtype,View on GitHub (pinned to 5d8ebabf11)
Solutions
- Call `from_strings_and_dtype_strict` only on a Categorical/Enum chunked array; verify the dtype first.
- Build a CategoricalChunkedBuilder from the string values instead of calling this on a non-categorical array.
Defensive patterns
Strategy: validation
When it happens
Trigger: This panic/expect fires when execution reaches an unguarded state described by: "from_strings_and_dtype_strict 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.
Common situations: Encountered when input data or configuration does not meet the preconditions of the throwing code path ("from_strings_and_dtype_strict 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.
AI-assisted analysis of pola-rs/polars@5d8ebabf11 (2026-08-19).
Data as JSON: /api/errors/4be0c5b6d05041ae.
Report an issue: GitHub.