{"record":{"id":"01e728ba14d75622","repo":"pola-rs/polars","slug":"cannot-create-date-from-other-type-dtype","errorCode":null,"errorMessage":"cannot create date from other type. dtype: {}","messagePattern":"cannot create date from other type\\. dtype: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":953,"sourceCode":"    T: Into<AnyValue<'a>>,\n{\n    #[inline]\n    fn from(a: Option<T>) -> Self {\n        match a {\n            None => AnyValue::Null,\n            Some(v) => v.into(),\n        }\n    }\n}\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,","sourceCodeStart":935,"sourceCodeEnd":971,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L935-L971","documentation":"AnyValue::as_date() reinterprets a scalar as a Date and only accepts AnyValue::Int32 (the physical days-since-epoch representation) or Null. Any other variant panics with its actual dtype, since converting e.g. a string or i64 to Date would be ambiguous.","triggerScenarios":"Calling as_date() (pub(crate) — reached via public APIs that coerce literals/scalars to dates) with an AnyValue of type Int64, String, Float, etc., instead of Int32 or Null.","commonSituations":"Passing a Python int or str as a date literal into a date-typed expression; fill_null/filter with a literal whose physical type doesn't match the Date column's i32 days representation; bindings auto-converting scalars without a cast.","solutions":["Provide the literal in the correct physical form (i32 days since epoch) or cast the Series/literal first: lit(value).cast(DataType::Date).","Convert strings with str.to_date(format) rather than feeding them through scalar coercion.","If you have an i64 timestamp, cast to Datetime and then .cast(DataType::Date).","Check the input value's type before the call and reject/convert non-Int32 values in the caller."],"exampleFix":"// before\nlet av: AnyValue<'_> = AnyValue::Int64(days); // wrong width\nlet d = av.as_date(); // panics\n// after\nlet av: AnyValue<'_> = AnyValue::Int32(days as i32);\nlet d = av.as_date();\n// or in expressions: lit(value).cast(DataType::Date)","handlingStrategy":"validation","validationCode":"// scalar must be Int32 (days) or Null before as_date\nif !matches!(av, AnyValue::Int32(_) | AnyValue::Null) {\n    return Err(PolarsError::ComputeError(\"date literal must be i32 days or Null\".into()));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Coerce literals at expression level: lit(v).cast(DataType::Date)","Parse date strings with str.to_date, not scalar coercion","Remember Date's physical repr is i32 days since epoch"],"tags":["polars","panic","date","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"}