{"record":{"id":"604e2035f3bbf67d","repo":"pola-rs/polars","slug":"can-only-convert-date-datetime-to-naivetime","errorCode":null,"errorMessage":"can only convert date/datetime to NaiveTime","messagePattern":"can only convert date/datetime to NaiveTime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/chunked_array/temporal/conversion.rs","lineNumber":32,"sourceCode":"            #[cfg(feature = \"dtype-date\")]\n            AnyValue::Date(v) => date32_to_datetime(*v),\n            #[cfg(feature = \"dtype-datetime\")]\n            AnyValue::Datetime(v, tu, _) => match tu {\n                TimeUnit::Nanoseconds => timestamp_ns_to_datetime(*v),\n                TimeUnit::Microseconds => timestamp_us_to_datetime(*v),\n                TimeUnit::Milliseconds => timestamp_ms_to_datetime(*v),\n            },\n            _ => panic!(\"can only convert date/datetime to NaiveDateTime\"),\n        }\n    }\n}\n\nimpl From<&AnyValue<'_>> for NaiveTime {\n    fn from(v: &AnyValue) -> Self {\n        match v {\n            #[cfg(feature = \"dtype-time\")]\n            AnyValue::Time(v) => time64ns_to_time(*v),\n            _ => panic!(\"can only convert date/datetime to NaiveTime\"),\n        }\n    }\n}\n\n// Used by lazy for literal conversion\npub fn datetime_to_timestamp_ns(v: NaiveDateTime) -> i64 {\n    let us = v.and_utc().timestamp() * 1_000_000_000; //nanos\n    us + v.and_utc().timestamp_subsec_nanos() as i64\n}\n\npub fn datetime_to_timestamp_ms(v: NaiveDateTime) -> i64 {\n    v.and_utc().timestamp_millis()\n}\n\npub fn datetime_to_timestamp_us(v: NaiveDateTime) -> i64 {\n    let us = v.and_utc().timestamp() * 1_000_000;\n    us + v.and_utc().timestamp_subsec_micros() as i64\n}","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/chunked_array/temporal/conversion.rs#L14-L50","documentation":"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.","triggerScenarios":"Calling .into() / NaiveTime::from(&AnyValue) on an AnyValue that is not AnyValue::Time — e.g. an Int64, Datetime, or Null value.","commonSituations":"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.","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."],"exampleFix":"// before\nlet t: NaiveTime = (&any_value).into(); // panics for non-Time values\n// after\nlet t = match any_value {\n    AnyValue::Time(v) => polars_core::chunked_array::temporal::time64ns_to_time(v),\n    _ => return Err(PolarsError::ComputeError(\"expected a Time value\".into())),\n};","handlingStrategy":"type-guard","validationCode":"fn is_time(av: &AnyValue) -> bool {\n    matches!(av, AnyValue::Time(_))\n}","typeGuard":"if !matches!(av, AnyValue::Time(_)) { return Err(...); }","tryCatchPattern":"Guard variant before conversion:\nlet t = match av { AnyValue::Time(_) => NaiveTime::from(&av), _ => bail!(\"not a Time value\") };","preventionTips":["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"],"tags":["polars","panic","temporal","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}