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 converts only Int64 physical values (and Null) to a Duration value; other dtypes panic naming the dtype. Means a duration reinterpretation was requested from a value that isn't the Int64 physical duration — cast the column to Duration instead.
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 68506541d2)
Solutions
- Construct Duration AnyValues only from Int64 values plus a TimeUnit.
- Cast the source value to Int64 in the desired time unit first.
Defensive patterns
Strategy: type-guard
When it happens
Trigger: This panic/expect fires when execution reaches an unguarded state described by: "cannot create duration from other type. dtype: {}". 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 ("cannot create duration from other type. dtype: {}"). 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@68506541d2 (2026-08-19).
Data as JSON: /api/errors/d24cc4f2336524c1.
Report an issue: GitHub.