pola-rs/polars · error

can only convert date/datetime to NaiveDateTime

Error message

can only convert date/datetime to NaiveDateTime

What it means

Panic in From<&AnyValue> for NaiveDateTime: the AnyValue variant is not Date or Datetime (e.g. a Time, Duration, or string), so there is no meaningful datetime conversion. Match on the variant or convert via the proper accessor first.

Source

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

use crate::prelude::*;

pub(crate) const NS_IN_DAY: i64 = 86_400_000_000_000;
pub(crate) const US_IN_DAY: i64 = 86_400_000_000;
pub(crate) const MS_IN_DAY: i64 = 86_400_000;
pub(crate) const SECONDS_IN_DAY: i64 = 86_400;

impl From<&AnyValue<'_>> for NaiveDateTime {
    fn from(v: &AnyValue) -> Self {
        match v {
            #[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

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. Convert only Date or Datetime columns to NaiveDateTime; check the dtype before conversion.
  2. Cast the column to Datetime first if the source dtype is compatible.
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 NaiveDateTime". 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 NaiveDateTime"). 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/6f3f05bcd1a382d5. Report an issue: GitHub.