pola-rs/polars · error
can not get dtype of Enum AnyValue
Error message
can not get dtype of Enum AnyValue
What it means
AnyValue::dtype() panics for Enum/EnumOwned scalars. As with categoricals, an enum scalar holds only the category index and a reference to the frozen categories, so a faithful DataType::Enum cannot be produced cheaply and the arm is unimplemented.
Source
Thrown at crates/polars-core/src/datatypes/any_value.rs:271
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()),
}
}
/// Extract a numerical value from the AnyValue
#[doc(hidden)]View on GitHub (pinned to df599052da)
Solutions
- Read the dtype from the Series or schema rather than from an individual value
- Convert the scalar to String when only the value is needed
- Handle the Enum(_, _) AnyValue variant explicitly in Rust code that must inspect it
Example fix
# before av = df["grade"].get(0) # grade is pl.Enum av.dtype() # panics # after df["grade"].dtype # Enum dtype from the column av.cast(pl.String) # value-only use
Defensive patterns
Strategy: validation
Validate before calling
if isinstance(df["grade"].dtype, pl.Enum):
dtype = df["grade"].dtype
else:
dtype = df["grade"].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
return series.get(idx).dtype() Prevention
- Keep enum dtype information in the schema, not in extracted scalars
- Treat pl.Enum columns specially in generic scalar-introspection code
When it happens
Trigger: Calling .dtype() on an AnyValue obtained from an Enum column (pl.Enum([...])): df["grade"].get(0).dtype(), or library code that maps scalars back to dtypes (e.g. inferring a schema from Python-side values).
Common situations: Schema-from-scalars utilities, generic scalar introspection after switching a column to pl.Enum for memory or correctness reasons.
Related errors
- can not get dtype of Categorical AnyValue
- comparing enums with different FrozenCategories is not suppo
- can't order enums from different FrozenCategories
- not implemented
- not implemented
AI-assisted analysis of pola-rs/polars@df599052da (2026-08-16).
Data as JSON: /api/errors/73c60a67a709a81f.
Report an issue: GitHub.