pola-rs/polars · error

cannot create time from other type. dtype

Error message

cannot create time from other type. dtype: {}

What it means

`AnyValue::as_time` converts an AnyValue into a Time value, whose physical storage is Int64 (nanoseconds since midnight). Only Int64 and Null are valid; anything else panics with the offending dtype. This guards against misrepresenting arbitrary values as times of day.

Solutions

  1. Convert strings to Time via `str.to_time` (strptime) rather than a direct cast
  2. Cast via a compatible intermediate: Datetime → Time should go through the dedicated `.dt` namespace, not raw AnyValue conversion
  3. Verify the physical dtype is Int64 before the conversion and handle Null explicitly

Example fix

// before
let t = av.as_time(); // panics for AnyValue::Utf8("12:30:00")
// after
let t = match av {
    AnyValue::Int64(_) | AnyValue::Null => av.as_time(),
    AnyValue::Utf8(s) => /* parse with NaiveTime::parse_from_str */ AnyValue::Null,
    _ => AnyValue::Null,
};
Defensive patterns

Strategy: validation

Validate before calling

fn can_cast_to_time(av: &AnyValue) -> bool {
    matches!(av, AnyValue::Int64(_) | AnyValue::Null)
}
if !can_cast_to_time(&av) { /* parse string with NaiveTime first */ }

Type guard

fn is_int64_or_null(av: &AnyValue) -> bool {
    matches!(av, AnyValue::Int64(_) | AnyValue::Null)
}

Prevention

When it happens

Trigger: Calling `as_time()` on an AnyValue that is not `AnyValue::Int64` or `AnyValue::Null` — e.g. casting a string ('12:30:00') or datetime column directly to Time at the AnyValue level.

Common situations: `.cast(DataType::Time)` on string columns read from CSV; converting Datetime columns to Time without first extracting the integer nanosecond representation.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/fb250b56c1a2b17d. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-core/src/datatypes/any_value.rs:1002

    #[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()),
            AnyValue::UInt16(v) => Some((*v).into()),
            AnyValue::UInt32(v) => Some((*v).into()),
            AnyValue::UInt64(v) => Some((*v).into()),
            AnyValue::Int8(v) => Some((*v).into()),
            AnyValue::Int16(v) => Some((*v).into()),
            AnyValue::Int32(v) => Some((*v).into()),
            AnyValue::Int64(v) => Some((*v).into()),
            AnyValue::Int128(v) => Some(*v),
            _ => None,
        }
    }

View on GitHub (pinned to fe841f959e)