pola-rs/polars · error

Invalid time unit ' ' for Duration.

Error message

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

What it means

During JSON serialization, Arrow Duration arrays are converted to human-readable durations only for Nanosecond, Microsecond, and Millisecond units. Any other time unit falls into the catch-all arm and panics. This guards against duration arrays with units polars-json has no converter for.

Solutions

  1. Cast the duration array to a supported unit before serializing: arr.cast_to(&ArrowDataType::Duration(TimeUnit::Microsecond)).
  2. Convert manually (multiply to ns/us/ms) if a cast path is unavailable.
  3. Ensure producers only emit Duration with Nanosecond/Microsecond/Millisecond units.
  4. Wrap serialization in catch_unwind if foreign data cannot be pre-validated.

Example fix

// before
let arr = ...; // Duration(TimeUnit::Second)
writer.serialize(&df);
// after
let arr = arr.cast_to(&ArrowDataType::Duration(TimeUnit::Millisecond)).unwrap();
writer.serialize(&df);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Serializing (via new_serializer's callers: JsonWriter::new/serialize/serialize_to_utf8, struct/list serializers) an array of DataType::Duration with a unit other than ns/us/ms — e.g. Duration(TimeUnit::Second) built manually or received from an external Arrow producer.

Common situations: Interoperating with other Arrow implementations that emit second-precision durations; hand-constructed arrays in tests; mismatched arrow-crate versions with differing TimeUnit sets.

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/d8e100318ca4fcfc. Report an issue: GitHub.

Appendix: source

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

                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,
                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,

View on GitHub (pinned to fe841f959e)