pola-rs/polars · error

Invalid time unit ' ' for Datetime.

Error message

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

What it means

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.

Solutions

  1. 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)).
  2. Convert the unit with polars_time converters (e.g. timestamp_s_to_datetime) if the data is genuinely second-precision.
  3. If building arrays manually, always use TimeUnit::Nanosecond or Microsecond for Timestamps.
  4. Audit FFI/external producers so no non-standard time unit crosses into polars-json serialization.

Example fix

// before
let arr = ...; // Timestamp(TimeUnit::Second, None)
writer.serialize(&df);
// after
let arr = arr.cast_to(&ArrowDataType::Timestamp(TimeUnit::Microsecond, None)).unwrap();
writer.serialize(&df);
Defensive patterns

Strategy: validation

Validate before calling

fn is_json_serializable_timestamp(dt: &ArrowDataType) -> bool {
    matches!(dt, ArrowDataType::Timestamp(TimeUnit::Nanosecond | TimeUnit::Microsecond | TimeUnit::Millisecond, _))
}
assert!(is_json_serializable_timestamp(arr.data_type()), "cast timestamp to ns/us/ms before JSON serialization");

Type guard

fn has_supported_time_unit(dt: &ArrowDataType) -> bool {
    matches!(dt, ArrowDataType::Timestamp(tu, _) if matches!(tu, TimeUnit::Nanosecond | TimeUnit::Microsecond | TimeUnit::Millisecond))
}

Prevention

When it happens

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

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

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

                dictionary_utf8view_serializer::<u32>(array, offset, take)
            },
            _ => {
                // Not produced by polars
                unreachable!()
            },
        },
        ArrowDataType::Date32 => date_serializer(
            array.as_any().downcast_ref().unwrap(),
            date32_to_date,
            offset,
            take,
        ),
        ArrowDataType::Timestamp(tu, None) => {
            let convert = match tu {
                TimeUnit::Nanosecond => timestamp_ns_to_datetime,
                TimeUnit::Microsecond => timestamp_us_to_datetime,
                TimeUnit::Millisecond => timestamp_ms_to_datetime,
                tu => panic!("Invalid time unit '{tu:?}' for Datetime."),
            };
            timestamp_serializer(
                array.as_any().downcast_ref().unwrap(),
                convert,
                offset,
                take,
            )
        },
        ArrowDataType::Timestamp(time_unit, Some(tz)) => timestamp_tz_serializer(
            array.as_any().downcast_ref().unwrap(),
            *time_unit,
            tz,
            offset,
            take,
        ),
        ArrowDataType::Duration(tu) => {
            let convert = match tu {
                TimeUnit::Nanosecond => duration_ns_to_duration,

View on GitHub (pinned to fe841f959e)