{"record":{"id":"9c798d105b7d4ac2","repo":"pola-rs/polars","slug":"invalid-time-unit-tu-for-time","errorCode":null,"errorMessage":"Invalid time unit '{tu:?}' for Time.","messagePattern":"Invalid time unit '(.+?)' for Time\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-json/src/json/write/serialize.rs","lineNumber":624,"sourceCode":"        ),\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,\n            )\n        },\n        ArrowDataType::Null => null_serializer(array.len(), offset, take),\n        other => todo!(\"Writing {:?} to JSON\", other),\n    }\n}\n\nfn serialize_item<'a>(\n    buffer: &mut Vec<u8>,\n    record: impl Iterator<Item = (&'a str, &'a [u8])>,\n    is_first_row: bool,\n) {","sourceCodeStart":606,"sourceCodeEnd":642,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-json/src/json/write/serialize.rs#L606-L642","documentation":"JSON serialization of a Time64 array only supports TimeUnit::Nanosecond (via time64ns_to_time). Any other time unit — e.g. Microsecond or Millisecond — hits the panic arm because polars-json defines no converter for them.","triggerScenarios":"Serializing an array of ArrowDataType::Time64 with a time unit other than Nanosecond through the JSON writer (new/serialize/serialize_to_utf8 or nested struct/list serializers).","commonSituations":"Receiving us/ms-precision Time64 arrays from other Arrow ecosystems (e.g. Python pyarrow defaults to us) and writing them to JSON with polars-json; manually built test arrays.","solutions":["Cast to nanosecond time before serializing: arr.cast_to(&ArrowDataType::Time64(TimeUnit::Nanosecond)).","Convert Time64(us/ms) values to ns by scaling the underlying buffer if cast is unavailable.","Prefer converting the data to a Datetime or String column and serialize that.","Validate the time unit of incoming FFI/IPC data before handing it to the JSON writer."],"exampleFix":"// before\nlet arr = ...; // Time64(TimeUnit::Microsecond)\nwriter.serialize(&df);\n// after\nlet arr = arr.cast_to(&ArrowDataType::Time64(TimeUnit::Nanosecond)).unwrap();\nwriter.serialize(&df);","handlingStrategy":"validation","validationCode":"fn time64_is_ns(dt: &ArrowDataType) -> bool {\n    matches!(dt, ArrowDataType::Time64(TimeUnit::Nanosecond))\n}\nlet arr = if time64_is_ns(arr.data_type()) { arr } else { arr.cast_to(&ArrowDataType::Time64(TimeUnit::Nanosecond)).unwrap() };","typeGuard":"fn is_time64_ns(dt: &ArrowDataType) -> bool {\n    matches!(dt, ArrowDataType::Time64(TimeUnit::Nanosecond))\n}","tryCatchPattern":null,"preventionTips":["Only build Time64 arrays with TimeUnit::Nanosecond when targeting JSON output.","Cast us/ms-precision time arrays (e.g. from pyarrow) to ns before handing to polars-json.","Validate incoming IPC schemas for Time64 units."],"tags":["arrow","json-serialization","time-unit","panic"],"backgroundTag":"unsupported-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"}