{"record":{"id":"6f3f05bcd1a382d5","repo":"pola-rs/polars","slug":"can-only-convert-date-datetime-to-naivedatetime","errorCode":null,"errorMessage":"can only convert date/datetime to NaiveDateTime","messagePattern":"can only convert date/datetime to NaiveDateTime","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/chunked_array/temporal/conversion.rs","lineNumber":22,"sourceCode":"use crate::prelude::*;\n\npub(crate) const NS_IN_DAY: i64 = 86_400_000_000_000;\npub(crate) const US_IN_DAY: i64 = 86_400_000_000;\npub(crate) const MS_IN_DAY: i64 = 86_400_000;\npub(crate) const SECONDS_IN_DAY: i64 = 86_400;\n\nimpl From<&AnyValue<'_>> for NaiveDateTime {\n    fn from(v: &AnyValue) -> Self {\n        match v {\n            #[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","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/pola-rs/polars/blob/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-core/src/chunked_array/temporal/conversion.rs#L4-L40","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert only Date or Datetime columns to NaiveDateTime; check the dtype before conversion.","Cast the column to Datetime first if the source dtype is compatible."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}