{"record":{"id":"713134f41f4e2713","repo":"pola-rs/polars","slug":"cannot-create-datetime-from-other-type-dtype","errorCode":null,"errorMessage":"cannot create datetime from other type. dtype: {}","messagePattern":"cannot create datetime from other type\\. dtype: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":962,"sourceCode":"}\n\nimpl<'a> AnyValue<'a> {\n    #[cfg(any(feature = \"dtype-date\", feature = \"dtype-datetime\"))]\n    pub(crate) fn as_date(&self) -> AnyValue<'static> {\n        match self {\n            #[cfg(feature = \"dtype-date\")]\n            AnyValue::Int32(v) => AnyValue::Date(*v),\n            AnyValue::Null => AnyValue::Null,\n            av => panic!(\"cannot create date from other type. dtype: {}\", av.dtype()),\n        }\n    }\n\n    #[cfg(feature = \"dtype-datetime\")]\n    pub(crate) fn as_datetime(&self, tu: TimeUnit, tz: Option<&'a TimeZone>) -> AnyValue<'a> {\n        match self {\n            AnyValue::Int64(v) => AnyValue::Datetime(*v, tu, tz),\n            AnyValue::Null => AnyValue::Null,\n            av => panic!(\n                \"cannot create datetime from other type. dtype: {}\",\n                av.dtype()\n            ),\n        }\n    }\n\n    #[cfg(feature = \"dtype-datetime\")]\n    pub(crate) fn as_datetime_owned(\n        &self,\n        tu: TimeUnit,\n        tz: Option<Arc<TimeZone>>,\n    ) -> AnyValue<'static> {\n        match self {\n            AnyValue::Int64(v) => AnyValue::DatetimeOwned(*v, tu, tz),\n            AnyValue::Null => AnyValue::Null,\n            av => panic!(\n                \"cannot create datetime from other type. dtype: {}\",\n                av.dtype()","sourceCodeStart":944,"sourceCodeEnd":980,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L944-L980","documentation":"AnyValue::as_datetime() coerces a scalar into a Datetime AnyValue and only accepts AnyValue::Int64 (the physical timestamp count) or Null. Any other variant (including Int32) panics, reporting the value's dtype, because the physical timestamp representation must be i64.","triggerScenarios":"Calling as_datetime(tu, tz) with an AnyValue::Int32, String, or Float — e.g. an int32 epoch seconds literal passed where an i64 microsecond timestamp is expected.","commonSituations":" supplying epoch values via bindings where they arrive as i32 or float; fill/filter literals not matching a Datetime column's i64 representation; mixing Date (i32) values with datetime coercion paths.","solutions":["Provide the literal as i64 in the column's TimeUnit (cast your i32/f64 to i64 first), or use lit(value).cast(DataType::Datetime(tu, tz)).","Parse strings with str.to_datetime instead of scalar coercion.","If starting from Date values, cast to Datetime at the expression level rather than calling as_datetime on the scalar.","Validate the AnyValue variant is Int64/Null before calling as_datetime in plugin code."],"exampleFix":"// before\nlet av: AnyValue<'_> = AnyValue::Int32(ts); // wrong width\nlet d = av.as_datetime(TimeUnit::Microseconds, None); // panics\n// after\nlet av: AnyValue<'_> = AnyValue::Int64(ts as i64);\nlet d = av.as_datetime(TimeUnit::Microseconds, None);\n// or in expressions: lit(ts).cast(DataType::Datetime(TimeUnit::Microseconds, None))","handlingStrategy":"validation","validationCode":"// scalar must be Int64 (timestamp) or Null before as_datetime\nif !matches!(av, AnyValue::Int64(_) | AnyValue::Null) {\n    return Err(PolarsError::ComputeError(\"datetime literal must be i64 timestamp or Null\".into()));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cast literals with lit(v).cast(DataType::Datetime(tu, tz))","Convert i32/f64 timestamps to i64 before scalar coercion","Parse datetime strings with str.to_datetime instead"],"tags":["polars","panic","datetime","literal","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"}