pola-rs/polars · error
can only convert date/datetime to NaiveTime
Error message
can only convert date/datetime to NaiveTime
What it means
The From<&AnyValue> for NaiveTime impl only handles AnyValue::Time (enabled with the 'dtype-time' feature). Every other variant hits a panic, because there is no sensible way to reinterpret another physical type as a time-of-day.
Solutions
- Cast the series to DataType::Time before reading values (series.cast(&DataType::Time)).
- Ensure the 'dtype-time' cargo feature is enabled if you expect Time AnyValues.
- Match on AnyValue::Time explicitly and handle other variants as an error instead of using the blanket From impl.
- For Datetime values, call .time() on the NaiveDateTime instead of converting the whole value to NaiveTime.
Example fix
// before
let t: NaiveTime = (&any_value).into(); // panics for non-Time values
// after
let t = match any_value {
AnyValue::Time(v) => polars_core::chunked_array::temporal::time64ns_to_time(v),
_ => return Err(PolarsError::ComputeError("expected a Time value".into())),
}; Defensive patterns
Strategy: type-guard
Validate before calling
fn is_time(av: &AnyValue) -> bool {
matches!(av, AnyValue::Time(_))
} Type guard
if !matches!(av, AnyValue::Time(_)) { return Err(...); } Try / catch
Guard variant before conversion:
let t = match av { AnyValue::Time(_) => NaiveTime::from(&av), _ => bail!("not a Time value") }; Prevention
- Only use the NaiveTime From impl after confirming the column dtype is Time
- Enable the dtype-time feature when working with time columns
- Cast to DataType::Time before value extraction
When it happens
Trigger: Calling .into() / NaiveTime::from(&AnyValue) on an AnyValue that is not AnyValue::Time — e.g. an Int64, Datetime, or Null value.
Common situations: Extracting elements from a Time column that was actually stored as Int64 nanoseconds without casting; pulling a literal into a time context in lazy code; feature 'dtype-time' absent so even Time values fall into the panic arm.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- can only convert date/datetime to NaiveDateTime
- cannot create date from other type. dtype
- cannot create datetime from other type. dtype
- could not cast
- invalid call to `get_strftime_format`; fmt=
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/604e2035f3bbf67d.
Report an issue: GitHub.
Appendix: 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 fe841f959e)