{"record":{"id":"d24cc4f2336524c1","repo":"pola-rs/polars","slug":"cannot-create-duration-from-other-type-dtype","errorCode":null,"errorMessage":"cannot create duration from other type. dtype: {}","messagePattern":"cannot create duration from other type\\. dtype: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":990,"sourceCode":"        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()\n            ),\n        }\n    }\n\n    #[cfg(feature = \"dtype-duration\")]\n    pub(crate) fn as_duration(&self, tu: TimeUnit) -> AnyValue<'static> {\n        match self {\n            AnyValue::Int64(v) => AnyValue::Duration(*v, tu),\n            AnyValue::Null => AnyValue::Null,\n            av => panic!(\n                \"cannot create duration from other type. dtype: {}\",\n                av.dtype()\n            ),\n        }\n    }\n\n    #[cfg(feature = \"dtype-time\")]\n    pub(crate) fn as_time(&self) -> AnyValue<'static> {\n        match self {\n            AnyValue::Int64(v) => AnyValue::Time(*v),\n            AnyValue::Null => AnyValue::Null,\n            av => panic!(\"cannot create time from other type. dtype: {}\", av.dtype()),\n        }\n    }\n\n    pub(crate) fn to_i128(&self) -> Option<i128> {\n        match self {\n            AnyValue::UInt8(v) => Some((*v).into()),","sourceCodeStart":972,"sourceCodeEnd":1008,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L972-L1008","documentation":"`AnyValue::as_duration` reinterprets an AnyValue as a Duration, which is physically an Int64 count of time units. Only Int64 and Null are accepted; any other variant panics with its dtype name. It exists to prevent silently misinterpreting non-integer values as durations.","triggerScenarios":"Calling `as_duration(tu)` on an AnyValue that is not `AnyValue::Int64` or `AnyValue::Null` — e.g. casting a string, float, or date column directly to Duration.","commonSituations":"`col('duration').cast(DataType::Duration(TimeUnit::Ms))` where the column contains strings or floats; aggregating/adding columns whose physical dtype drifted after IO (CSV reads infer strings).","solutions":["Cast the column to Int64 first (or parse durations from strings) before converting to Duration","Use string → duration parsing utilities instead of a raw cast for text columns","Filter or fill non-integer values (e.g. cast with strict=False / fill Null) before the conversion"],"exampleFix":"// before\nlet d = av.as_duration(TimeUnit::Milliseconds); // panics for AnyValue::Float64\n// after\nlet d = match av {\n    AnyValue::Int64(_) | AnyValue::Null => av.as_duration(TimeUnit::Milliseconds),\n    av => av.cast(&DataType::Int64).as_duration(TimeUnit::Milliseconds),\n};","handlingStrategy":"validation","validationCode":"fn can_cast_to_duration(av: &AnyValue) -> bool {\n    matches!(av, AnyValue::Int64(_) | AnyValue::Null)\n}\nif !can_cast_to_duration(&av) { /* cast to Int64 or parse first */ }","typeGuard":"fn is_int64_or_null(av: &AnyValue) -> bool {\n    matches!(av, AnyValue::Int64(_) | AnyValue::Null)\n}","tryCatchPattern":null,"preventionTips":["Cast strings/floats to Int64 before Duration conversion","Use str-to-duration parsing for text columns","Keep duration columns' physical dtype as Int64"],"tags":["rust","polars","duration","dtype"],"backgroundTag":"unsupported-dtype","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"}