{"record":{"id":"d8e100318ca4fcfc","repo":"pola-rs/polars","slug":"invalid-time-unit-tu-for-duration","errorCode":null,"errorMessage":"Invalid time unit '{tu:?}' for Duration.","messagePattern":"Invalid time unit '(.+?)' for Duration\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-json/src/json/write/serialize.rs","lineNumber":612,"sourceCode":"                array.as_any().downcast_ref().unwrap(),\n                convert,\n                offset,\n                take,\n            )\n        },\n        ArrowDataType::Timestamp(time_unit, Some(tz)) => timestamp_tz_serializer(\n            array.as_any().downcast_ref().unwrap(),\n            *time_unit,\n            tz,\n            offset,\n            take,\n        ),\n        ArrowDataType::Duration(tu) => {\n            let convert = match tu {\n                TimeUnit::Nanosecond => duration_ns_to_duration,\n                TimeUnit::Microsecond => duration_us_to_duration,\n                TimeUnit::Millisecond => duration_ms_to_duration,\n                tu => panic!(\"Invalid time unit '{tu:?}' for Duration.\"),\n            };\n            duration_serializer(\n                array.as_any().downcast_ref().unwrap(),\n                convert,\n                offset,\n                take,\n            )\n        },\n        ArrowDataType::Time64(tu) => {\n            let convert = match tu {\n                TimeUnit::Nanosecond => time64ns_to_time,\n                tu => panic!(\"Invalid time unit '{tu:?}' for Time.\"),\n            };\n            time_serializer(\n                array.as_any().downcast_ref().unwrap(),\n                convert,\n                offset,\n                take,","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-json/src/json/write/serialize.rs#L594-L630","documentation":"During JSON serialization, Arrow Duration arrays are converted to human-readable durations only for Nanosecond, Microsecond, and Millisecond units. Any other time unit falls into the catch-all arm and panics. This guards against duration arrays with units polars-json has no converter for.","triggerScenarios":"Serializing (via new_serializer's callers: JsonWriter::new/serialize/serialize_to_utf8, struct/list serializers) an array of DataType::Duration with a unit other than ns/us/ms — e.g. Duration(TimeUnit::Second) built manually or received from an external Arrow producer.","commonSituations":"Interoperating with other Arrow implementations that emit second-precision durations; hand-constructed arrays in tests; mismatched arrow-crate versions with differing TimeUnit sets.","solutions":["Cast the duration array to a supported unit before serializing: arr.cast_to(&ArrowDataType::Duration(TimeUnit::Microsecond)).","Convert manually (multiply to ns/us/ms) if a cast path is unavailable.","Ensure producers only emit Duration with Nanosecond/Microsecond/Millisecond units.","Wrap serialization in catch_unwind if foreign data cannot be pre-validated."],"exampleFix":"// before\nlet arr = ...; // Duration(TimeUnit::Second)\nwriter.serialize(&df);\n// after\nlet arr = arr.cast_to(&ArrowDataType::Duration(TimeUnit::Millisecond)).unwrap();\nwriter.serialize(&df);","handlingStrategy":"validation","validationCode":"fn duration_supported(dt: &ArrowDataType) -> bool {\n    matches!(dt, ArrowDataType::Duration(tu) if matches!(tu, TimeUnit::Nanosecond | TimeUnit::Microsecond | TimeUnit::Millisecond))\n}","typeGuard":"fn has_supported_duration_unit(dt: &ArrowDataType) -> bool {\n    matches!(dt, ArrowDataType::Duration(tu) if matches!(tu, TimeUnit::Nanosecond | TimeUnit::Microsecond | TimeUnit::Millisecond))\n}","tryCatchPattern":null,"preventionTips":["Cast durations to Microsecond (a safe common denominator) before serialization.","Reject or convert external Arrow inputs at schema-validation time.","Keep arrow/polars crate versions aligned to avoid divergent TimeUnit definitions."],"tags":["arrow","json-serialization","time-unit","duration","panic"],"backgroundTag":"invalid-enum-value","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"}