pola-rs/polars · error

Invalid time unit ' ' for Time.

Error message

Invalid time unit '{tu:?}' for Time.

What it means

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.

Solutions

  1. Cast to nanosecond time before serializing: arr.cast_to(&ArrowDataType::Time64(TimeUnit::Nanosecond)).
  2. Convert Time64(us/ms) values to ns by scaling the underlying buffer if cast is unavailable.
  3. Prefer converting the data to a Datetime or String column and serialize that.
  4. Validate the time unit of incoming FFI/IPC data before handing it to the JSON writer.

Example fix

// before
let arr = ...; // Time64(TimeUnit::Microsecond)
writer.serialize(&df);
// after
let arr = arr.cast_to(&ArrowDataType::Time64(TimeUnit::Nanosecond)).unwrap();
writer.serialize(&df);
Defensive patterns

Strategy: validation

Validate before calling

fn time64_is_ns(dt: &ArrowDataType) -> bool {
    matches!(dt, ArrowDataType::Time64(TimeUnit::Nanosecond))
}
let arr = if time64_is_ns(arr.data_type()) { arr } else { arr.cast_to(&ArrowDataType::Time64(TimeUnit::Nanosecond)).unwrap() };

Type guard

fn is_time64_ns(dt: &ArrowDataType) -> bool {
    matches!(dt, ArrowDataType::Time64(TimeUnit::Nanosecond))
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/9c798d105b7d4ac2. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-json/src/json/write/serialize.rs:624

        ),
        ArrowDataType::Duration(tu) => {
            let convert = match tu {
                TimeUnit::Nanosecond => duration_ns_to_duration,
                TimeUnit::Microsecond => duration_us_to_duration,
                TimeUnit::Millisecond => duration_ms_to_duration,
                tu => panic!("Invalid time unit '{tu:?}' for Duration."),
            };
            duration_serializer(
                array.as_any().downcast_ref().unwrap(),
                convert,
                offset,
                take,
            )
        },
        ArrowDataType::Time64(tu) => {
            let convert = match tu {
                TimeUnit::Nanosecond => time64ns_to_time,
                tu => panic!("Invalid time unit '{tu:?}' for Time."),
            };
            time_serializer(
                array.as_any().downcast_ref().unwrap(),
                convert,
                offset,
                take,
            )
        },
        ArrowDataType::Null => null_serializer(array.len(), offset, take),
        other => todo!("Writing {:?} to JSON", other),
    }
}

fn serialize_item<'a>(
    buffer: &mut Vec<u8>,
    record: impl Iterator<Item = (&'a str, &'a [u8])>,
    is_first_row: bool,
) {

View on GitHub (pinned to fe841f959e)