{"record":{"id":"fb250b56c1a2b17d","repo":"pola-rs/polars","slug":"cannot-create-time-from-other-type-dtype","errorCode":null,"errorMessage":"cannot create time from other type. dtype: {}","messagePattern":"cannot create time from other type\\. dtype: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":1002,"sourceCode":"\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()),\n            AnyValue::UInt16(v) => Some((*v).into()),\n            AnyValue::UInt32(v) => Some((*v).into()),\n            AnyValue::UInt64(v) => Some((*v).into()),\n            AnyValue::Int8(v) => Some((*v).into()),\n            AnyValue::Int16(v) => Some((*v).into()),\n            AnyValue::Int32(v) => Some((*v).into()),\n            AnyValue::Int64(v) => Some((*v).into()),\n            AnyValue::Int128(v) => Some(*v),\n            _ => None,\n        }\n    }\n","sourceCodeStart":984,"sourceCodeEnd":1020,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L984-L1020","documentation":"`AnyValue::as_time` converts an AnyValue into a Time value, whose physical storage is Int64 (nanoseconds since midnight). Only Int64 and Null are valid; anything else panics with the offending dtype. This guards against misrepresenting arbitrary values as times of day.","triggerScenarios":"Calling `as_time()` on an AnyValue that is not `AnyValue::Int64` or `AnyValue::Null` — e.g. casting a string ('12:30:00') or datetime column directly to Time at the AnyValue level.","commonSituations":"`.cast(DataType::Time)` on string columns read from CSV; converting Datetime columns to Time without first extracting the integer nanosecond representation.","solutions":["Convert strings to Time via `str.to_time` (strptime) rather than a direct cast","Cast via a compatible intermediate: Datetime → Time should go through the dedicated `.dt` namespace, not raw AnyValue conversion","Verify the physical dtype is Int64 before the conversion and handle Null explicitly"],"exampleFix":"// before\nlet t = av.as_time(); // panics for AnyValue::Utf8(\"12:30:00\")\n// after\nlet t = match av {\n    AnyValue::Int64(_) | AnyValue::Null => av.as_time(),\n    AnyValue::Utf8(s) => /* parse with NaiveTime::parse_from_str */ AnyValue::Null,\n    _ => AnyValue::Null,\n};","handlingStrategy":"validation","validationCode":"fn can_cast_to_time(av: &AnyValue) -> bool {\n    matches!(av, AnyValue::Int64(_) | AnyValue::Null)\n}\nif !can_cast_to_time(&av) { /* parse string with NaiveTime first */ }","typeGuard":"fn is_int64_or_null(av: &AnyValue) -> bool {\n    matches!(av, AnyValue::Int64(_) | AnyValue::Null)\n}","tryCatchPattern":null,"preventionTips":["Use str.to_time for string → Time conversion","Use the .dt namespace for Datetime → Time conversion","Verify physical dtype is Int64 before AnyValue-level time casts"],"tags":["rust","polars","time","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"}