{"record":{"id":"23c3bb98bea4ff48","repo":"pola-rs/polars","slug":"invalid-time-unit-tu-for-datetime","errorCode":null,"errorMessage":"Invalid time unit '{tu:?}' for Datetime.","messagePattern":"Invalid time unit '(.+?)' for Datetime\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-json/src/json/write/serialize.rs","lineNumber":591,"sourceCode":"                dictionary_utf8view_serializer::<u32>(array, offset, take)\n            },\n            _ => {\n                // Not produced by polars\n                unreachable!()\n            },\n        },\n        ArrowDataType::Date32 => date_serializer(\n            array.as_any().downcast_ref().unwrap(),\n            date32_to_date,\n            offset,\n            take,\n        ),\n        ArrowDataType::Timestamp(tu, None) => {\n            let convert = match tu {\n                TimeUnit::Nanosecond => timestamp_ns_to_datetime,\n                TimeUnit::Microsecond => timestamp_us_to_datetime,\n                TimeUnit::Millisecond => timestamp_ms_to_datetime,\n                tu => panic!(\"Invalid time unit '{tu:?}' for Datetime.\"),\n            };\n            timestamp_serializer(\n                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,","sourceCodeStart":573,"sourceCodeEnd":609,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-json/src/json/write/serialize.rs#L573-L609","documentation":"When serializing an Arrow Timestamp array to JSON, polars-json matches on the TimeUnit but only handles Nanosecond, Microsecond, and Millisecond. Any other time unit reaches an unreachable match arm and panics. Since Arrow only defines these three units for Timestamp, hitting this indicates data with an unexpected/invalid time unit, typically from a hand-built or foreign-produced array.","triggerScenarios":"Calling polars-json's JsonWriter/serialize path (via `new`, `serialize`, `serialize_to_utf8`, or nested struct/list serializers) on an array whose DataType is Timestamp with a time unit other than Second/Nanosecond/Microsecond/Millisecond (e.g. a Second-precision timestamp, which has no converter here).","commonSituations":"Feeding Arrow arrays produced by an external library or FFI boundary that uses TimeUnit::Second; custom ArrowDataType construction in tests or IPC round-trips; version mismatches between arrow/polars crates where time-unit sets diverge.","solutions":["Cast the timestamp column to a supported unit before serializing: df.column(...).cast(&DataType::Datetime(TimeUnit::Microsecond, tz)) or arr.cast_to(&ArrowDataType::Timestamp(TimeUnit::Nanosecond, None)).","Convert the unit with polars_time converters (e.g. timestamp_s_to_datetime) if the data is genuinely second-precision.","If building arrays manually, always use TimeUnit::Nanosecond or Microsecond for Timestamps.","Audit FFI/external producers so no non-standard time unit crosses into polars-json serialization."],"exampleFix":"// before\nlet arr = ...; // Timestamp(TimeUnit::Second, None)\nwriter.serialize(&df);\n// after\nlet arr = arr.cast_to(&ArrowDataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();\nwriter.serialize(&df);","handlingStrategy":"validation","validationCode":"fn is_json_serializable_timestamp(dt: &ArrowDataType) -> bool {\n    matches!(dt, ArrowDataType::Timestamp(TimeUnit::Nanosecond | TimeUnit::Microsecond | TimeUnit::Millisecond, _))\n}\nassert!(is_json_serializable_timestamp(arr.data_type()), \"cast timestamp to ns/us/ms before JSON serialization\");","typeGuard":"fn has_supported_time_unit(dt: &ArrowDataType) -> bool {\n    matches!(dt, ArrowDataType::Timestamp(tu, _) if matches!(tu, TimeUnit::Nanosecond | TimeUnit::Microsecond | TimeUnit::Millisecond))\n}","tryCatchPattern":null,"preventionTips":["Always normalize timestamps to Nanosecond or Microsecond at data-ingestion boundaries.","Validate Arrow schemas coming from FFI/IPC before serializing.","Add unit tests that round-trip every TimeUnit through your JSON writer path."],"tags":["arrow","json-serialization","time-unit","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"}