pola-rs/polars · error
can not get dtype of Categorical AnyValue
Error message
can not get dtype of Categorical AnyValue
What it means
AnyValue::dtype() panics for Categorical/CategoricalOwned scalars. A Categorical AnyValue carries only the physical category index plus a borrowed reference to the categories; constructing a full DataType::Categorical would require deciding on and cloning the categories mapping, so the conversion is deliberately unimplemented.
Source
Thrown at crates/polars-core/src/datatypes/any_value.rs:268
Float32(_) => DataType::Float32,
Float64(_) => DataType::Float64,
String(_) | StringOwned(_) => DataType::String,
Binary(_) | BinaryOwned(_) => DataType::Binary,
#[cfg(feature = "dtype-date")]
Date(_) => DataType::Date,
#[cfg(feature = "dtype-time")]
Time(_) => DataType::Time,
#[cfg(feature = "dtype-datetime")]
Datetime(_, tu, tz) => DataType::Datetime(*tu, (*tz).cloned()),
#[cfg(feature = "dtype-datetime")]
DatetimeOwned(_, tu, tz) => {
DataType::Datetime(*tu, tz.as_ref().map(|v| v.as_ref().clone()))
},
#[cfg(feature = "dtype-duration")]
Duration(_, tu) => DataType::Duration(*tu),
#[cfg(feature = "dtype-categorical")]
Categorical(_, _) | CategoricalOwned(_, _) => {
unimplemented!("can not get dtype of Categorical AnyValue")
},
#[cfg(feature = "dtype-categorical")]
Enum(_, _) | EnumOwned(_, _) => unimplemented!("can not get dtype of Enum AnyValue"),
List(s) => DataType::List(Box::new(s.dtype().clone())),
#[cfg(feature = "dtype-array")]
Array(s, size) => DataType::Array(Box::new(s.dtype().clone()), *size),
#[cfg(feature = "dtype-struct")]
Struct(_, _, fields) => DataType::Struct(fields.to_vec()),
#[cfg(feature = "dtype-struct")]
StructOwned(payload) => DataType::Struct(payload.1.clone()),
#[cfg(feature = "dtype-decimal")]
Decimal(_, p, s) => DataType::Decimal(*p, *s),
#[cfg(feature = "object")]
Object(o) => DataType::Object(o.type_name()),
#[cfg(feature = "object")]
ObjectOwned(o) => DataType::Object(o.0.type_name()),
}
}View on GitHub (pinned to df599052da)
Solutions
- Get the dtype from the Series/Column instead of the scalar: df["cat"].dtype
- Extract the scalar as a string first if you only need its value: s.get(0).cast(pl.String) or str value accessors
- If you truly need a per-scalar dtype, pattern-match the AnyValue and handle Categorical(_, ref_map) yourself
Example fix
# before av = df["cat"].get(0) dt = av.dtype() # panics for Categorical AnyValue # after dt = df["cat"].dtype # dtype from the column, never panics
Defensive patterns
Strategy: validation
Validate before calling
# Ask the column, not the scalar
if isinstance(df["cat"].dtype, pl.Categorical):
dtype = df["cat"].dtype # safe
else:
dtype = df["cat"].get(0).dtype() Type guard
def scalar_dtype_safe(series: pl.Series, idx: int = 0):
if isinstance(series.dtype, (pl.Categorical, pl.Enum)):
return series.dtype # categorical/enum scalars cannot produce dtypes
return series.get(idx).dtype() Prevention
- Always read dtypes from Series/schema; never from individual AnyValues
- Cast categorical scalars to String when only the value matters
When it happens
Trigger: Introspecting the dtype of a scalar extracted from a Categorical column: s = df["cat"]; av = s.get(0); av.dtype() - or any library code path that calls AnyValue::dtype() on a value taken from a categorical/enum Series (scalar coercion, schema inference from scalars).
Common situations: Building schemas from sample values, generic scalar-handling utilities, or passing a single categorical value around and querying its type instead of the column's type.
Related errors
- can not get dtype of Enum AnyValue
- comparing categoricals with different Categories is not supp
- not implemented
- not implemented
- horizontal_flatten not supported for data type {:?}
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/cd4607a189ed4928.
Report an issue: GitHub.