pola-rs/polars · error

can only convert date/datetime to NaiveTime

Error message

can only convert date/datetime to NaiveTime

What it means

Panic in From<&AnyValue> for NaiveTime: the AnyValue variant is not a Time (or Datetime whose time component is taken); the value cannot be interpreted as a time of day. Match on the variant or extract the time explicitly.

Source

Thrown at crates/polars-core/src/chunked_array/temporal/conversion.rs:32

            #[cfg(feature = "dtype-date")]
            AnyValue::Date(v) => date32_to_datetime(*v),
            #[cfg(feature = "dtype-datetime")]
            AnyValue::Datetime(v, tu, _) => match tu {
                TimeUnit::Nanoseconds => timestamp_ns_to_datetime(*v),
                TimeUnit::Microseconds => timestamp_us_to_datetime(*v),
                TimeUnit::Milliseconds => timestamp_ms_to_datetime(*v),
            },
            _ => panic!("can only convert date/datetime to NaiveDateTime"),
        }
    }
}

impl From<&AnyValue<'_>> for NaiveTime {
    fn from(v: &AnyValue) -> Self {
        match v {
            #[cfg(feature = "dtype-time")]
            AnyValue::Time(v) => time64ns_to_time(*v),
            _ => panic!("can only convert date/datetime to NaiveTime"),
        }
    }
}

// Used by lazy for literal conversion
pub fn datetime_to_timestamp_ns(v: NaiveDateTime) -> i64 {
    let us = v.and_utc().timestamp() * 1_000_000_000; //nanos
    us + v.and_utc().timestamp_subsec_nanos() as i64
}

pub fn datetime_to_timestamp_ms(v: NaiveDateTime) -> i64 {
    v.and_utc().timestamp_millis()
}

pub fn datetime_to_timestamp_us(v: NaiveDateTime) -> i64 {
    let us = v.and_utc().timestamp() * 1_000_000;
    us + v.and_utc().timestamp_subsec_micros() as i64
}

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. Convert only Date/Datetime/Time columns to NaiveTime as supported; verify the dtype first.
  2. Cast to Time or Datetime before extracting the NaiveTime.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: This panic/expect fires when execution reaches an unguarded state described by: "can only convert date/datetime to NaiveTime". 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 ("can only convert date/datetime to NaiveTime"). 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@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/604e2035f3bbf67d. Report an issue: GitHub.