{"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/9b5d73fd00236295624374b075d16b1fe6ec6df9/crates/polars-json/src/json/write/serialize.rs#L606-L642","documentation":"For Time64 columns the JSON writer only implements the Nanosecond converter; Time64(TimeUnit::Microsecond) - Arrow's other legal 64-bit time-of-day unit - hits the catch-all panic. Time32 columns go through a different branch, so this is specifically 64-bit micro-precision time data.","triggerScenarios":"write_json on a column typed ArrowDataType::Time64(TimeUnit::Microsecond), e.g. arrays built with arrow-rs' Time64MicrosecondType from another engine's time-of-day output.","commonSituations":"arrow-rs interop; time-of-day columns produced by engines that store micros since midnight.","solutions":["Cast to Time64(Nanosecond) (or polars' DataType::Time, stored as ns i64) before write_json","Or store as Int64 microseconds / a formatted string","Inspect dtypes before exporting JSON"],"exampleFix":"// before: column typed Time64(Microsecond)\ndf.write_json(&mut out)?; // panic: Invalid time unit 'Microsecond' for Time.\n\n// after\nlet df = df.with_column(col(\"t\").cast(&DataType::Time))?; // polars Time = ns i64\ndf.write_json(&mut out)?;","handlingStrategy":"fallback","validationCode":"fn normalize_time64(df: &DataFrame) -> PolarsResult<DataFrame> {\n    let mut exprs = vec![];\n    for (name, dt) in df.schema().iter() {\n        if matches!(dt, DataType::Time64(TimeUnit::Microsecond)) {\n            exprs.push(col(name).cast(&DataType::Time).alias(name)); // polars Time = i64 ns\n        }\n    }\n    df.lazy().with_columns(exprs).collect()\n}\n// run before write_json","typeGuard":"fn has_writable_time64(df: &DataFrame) -> bool {\n    df.schema().iter_values().all(|dt| match dt {\n        DataType::Time64(tu) => matches!(tu, TimeUnit::Nanosecond),\n        _ => true,\n    })\n}","tryCatchPattern":"catch_unwind around write_json only converts the panic to an error; casting Time64(Microsecond) to nanosecond Time beforehand avoids the failing match arm.","preventionTips":["Convert arrow Time64(us) to Time64(ns)/polars Time at interop boundaries","Validate dtypes before JSON export in a pre-write pass","Keep a dtype matrix test for every writer your pipeline uses"],"tags":["polars","json","time","time-unit","arrow","serialization","panic"],"backgroundTag":"unsupported-time-unit","analyzedSha":"9b5d73fd00236295624374b075d16b1fe6ec6df9","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}