pola-rs/polars · error
cannot create duration from other type. dtype
Error message
cannot create duration from other type. dtype: {} What it means
`AnyValue::as_duration` reinterprets an AnyValue as a Duration, which is physically an Int64 count of time units. Only Int64 and Null are accepted; any other variant panics with its dtype name. It exists to prevent silently misinterpreting non-integer values as durations.
Solutions
- Cast the column to Int64 first (or parse durations from strings) before converting to Duration
- Use string → duration parsing utilities instead of a raw cast for text columns
- Filter or fill non-integer values (e.g. cast with strict=False / fill Null) before the conversion
Example fix
// before
let d = av.as_duration(TimeUnit::Milliseconds); // panics for AnyValue::Float64
// after
let d = match av {
AnyValue::Int64(_) | AnyValue::Null => av.as_duration(TimeUnit::Milliseconds),
av => av.cast(&DataType::Int64).as_duration(TimeUnit::Milliseconds),
}; Defensive patterns
Strategy: validation
Validate before calling
fn can_cast_to_duration(av: &AnyValue) -> bool {
matches!(av, AnyValue::Int64(_) | AnyValue::Null)
}
if !can_cast_to_duration(&av) { /* cast to Int64 or parse first */ } Type guard
fn is_int64_or_null(av: &AnyValue) -> bool {
matches!(av, AnyValue::Int64(_) | AnyValue::Null)
} Prevention
- Cast strings/floats to Int64 before Duration conversion
- Use str-to-duration parsing for text columns
- Keep duration columns' physical dtype as Int64
When it happens
Trigger: Calling `as_duration(tu)` on an AnyValue that is not `AnyValue::Int64` or `AnyValue::Null` — e.g. casting a string, float, or date column directly to Duration.
Common situations: `col('duration').cast(DataType::Duration(TimeUnit::Ms))` where the column contains strings or floats; aggregating/adding columns whose physical dtype drifted after IO (CSV reads infer strings).
Related errors
- activate dtype
- activate feature dtype-duration
- adding durations with different units is not supported here
- cannot create time from other type. dtype
- date not implemented for
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/d24cc4f2336524c1.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-core/src/datatypes/any_value.rs:990
tu: TimeUnit,
tz: Option<Arc<TimeZone>>,
) -> AnyValue<'static> {
match self {
AnyValue::Int64(v) => AnyValue::DatetimeOwned(*v, tu, tz),
AnyValue::Null => AnyValue::Null,
av => panic!(
"cannot create datetime from other type. dtype: {}",
av.dtype()
),
}
}
#[cfg(feature = "dtype-duration")]
pub(crate) fn as_duration(&self, tu: TimeUnit) -> AnyValue<'static> {
match self {
AnyValue::Int64(v) => AnyValue::Duration(*v, tu),
AnyValue::Null => AnyValue::Null,
av => panic!(
"cannot create duration from other type. dtype: {}",
av.dtype()
),
}
}
#[cfg(feature = "dtype-time")]
pub(crate) fn as_time(&self) -> AnyValue<'static> {
match self {
AnyValue::Int64(v) => AnyValue::Time(*v),
AnyValue::Null => AnyValue::Null,
av => panic!("cannot create time from other type. dtype: {}", av.dtype()),
}
}
pub(crate) fn to_i128(&self) -> Option<i128> {
match self {
AnyValue::UInt8(v) => Some((*v).into()),View on GitHub (pinned to fe841f959e)