pola-rs/polars · error

comparing durations with different units is not supported

Error message

comparing durations with different units is not supported

What it means

AnyValue::partial_cmp only supports comparing two Duration values that share the same TimeUnit. A Duration in milliseconds and one in nanoseconds store different integer scales, so direct index comparison would be wrong; the library panics with unimplemented!() when the units differ.

Solutions

  1. Cast both duration columns to the same TimeUnit before comparing (`.cast(&DataType::Duration(TimeUnit::Nanoseconds, None))`)
  2. Use polars expression-level comparisons which normalize units, rather than raw AnyValue::partial_cmp
  3. Standardize the time unit at parse/creation time so all duration columns in the workflow use one unit
  4. Convert durations to a fixed unit via `.dt.cast_time_unit(...)` in expressions

Example fix

// before
let d1 = Series::new("d1", &[1i64]).unwrap().cast(&DataType::Duration(TimeUnit::Milliseconds, None)).unwrap();
let d2 = Series::new("d2", &[1000i64]).unwrap().cast(&DataType::Duration(TimeUnit::Nanoseconds, None)).unwrap();
// comparing AnyValues of d1 vs d2 panics
// after
let d2 = d2.cast(&DataType::Duration(TimeUnit::Milliseconds, None)).unwrap();
let _ = d1.equal(&d2)?;
Defensive patterns

Strategy: validation

Validate before calling

fn durations_comparable(a: &AnyValue, b: &AnyValue) -> bool {
    match (a, b) {
        (AnyValue::Duration(_, lu), AnyValue::Duration(_, ru)) => lu == ru,
        _ => false,
    }
}

Prevention

When it happens

Trigger: Calling AnyValue::partial_cmp on two AnyValue::Duration values whose TimeUnit differs (lu != ru) — e.g. comparing durations from one column created with default us unit and another created/parsed with ms or ns unit.

Common situations: Mixing durations produced by datetime subtraction across columns with different time units, durations parsed from strings with different requested units, or interop with Arrow/other systems that emitted a different duration unit.

Related errors


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

Appendix: source

Thrown at crates/polars-core/src/datatypes/any_value.rs:1437

            (Float64(l), Float64(r)) => Some(l.tot_cmp(r)),
            (String(l), String(r)) => l.partial_cmp(r),
            (Binary(l), Binary(r)) => l.partial_cmp(r),
            #[cfg(feature = "dtype-date")]
            (Date(l), Date(r)) => l.partial_cmp(r),
            #[cfg(feature = "dtype-datetime")]
            (Datetime(lt, lu, lz), Datetime(rt, ru, rz)) => {
                if lu != ru || lz != rz {
                    unimplemented!(
                        "comparing datetimes with different units or timezones is not supported"
                    );
                }

                lt.partial_cmp(rt)
            },
            #[cfg(feature = "dtype-duration")]
            (Duration(lt, lu), Duration(rt, ru)) => {
                if lu != ru {
                    unimplemented!("comparing durations with different units is not supported");
                }

                lt.partial_cmp(rt)
            },
            #[cfg(feature = "dtype-time")]
            (Time(l), Time(r)) => l.partial_cmp(r),
            #[cfg(feature = "dtype-categorical")]
            (Categorical(l_cat, l_map), Categorical(r_cat, r_map)) => unsafe {
                let l_str = l_map.cat_to_str_unchecked(*l_cat);
                let r_str = r_map.cat_to_str_unchecked(*r_cat);
                l_str.partial_cmp(r_str)
            },
            #[cfg(feature = "dtype-categorical")]
            (Enum(l_cat, l_map), Enum(r_cat, r_map)) => {
                if !Arc::ptr_eq(l_map, r_map) {
                    unimplemented!("can't order enums from different FrozenCategories")
                }
                l_cat.partial_cmp(r_cat)

View on GitHub (pinned to fe841f959e)