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

  1. Cast the column to Int64 first (or parse durations from strings) before converting to Duration
  2. Use string → duration parsing utilities instead of a raw cast for text columns
  3. 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

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


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)