pola-rs/polars · error

adding durations with different units is not supported here

Error message

adding durations with different units is not supported here

What it means

`AnyValue::add` for Duration values requires both operands to share the same TimeUnit (ns/ms/us). Adding e.g. a millisecond duration to a nanosecond duration hits an `unimplemented!` panic, because the operation would require unit conversion that this code path does not perform.

Solutions

  1. Normalize both durations to the same TimeUnit (cast one column with `.cast(DataType::Duration(other_tu))`) before adding
  2. Align units at ingestion time so all duration columns use a single TimeUnit
  3. Perform the addition on the Series/ChunkedArray level where unit conversion is handled

Example fix

// before
let sum = av_ms.add(&av_ns); // unimplemented! panic
// after
let av_ns_as_ms = AnyValue::Duration(ns_value / 1_000_000, TimeUnit::Milliseconds);
let sum = av_ms.add(&av_ns_as_ms);
Defensive patterns

Strategy: validation

Validate before calling

fn can_add_durations(l: &AnyValue, r: &AnyValue) -> bool {
    match (l, r) {
        (AnyValue::Duration(_, lu), AnyValue::Duration(_, ru)) => lu == ru,
        _ => true,
    }
}

Type guard

fn same_time_unit(l: &AnyValue, r: &AnyValue) -> bool {
    matches!((l, r), (AnyValue::Duration(_, lu), AnyValue::Duration(_, ru)) if lu == ru)
}

Prevention

When it happens

Trigger: Using the `+` operator (Add impl) on two AnyValue::Duration values with different TimeUnits — e.g. `Duration(1, Ms) + Duration(1, Ns)`.

Common situations: Arithmetic between columns that were read from different sources with different time units (one parquet in ms, another CSV in us); mixing duration literals of different units in an expression.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

    }

    #[must_use]
    pub fn add(&self, rhs: &AnyValue) -> AnyValue<'static> {
        use AnyValue::*;
        match (self, rhs) {
            (Null, r) => r.clone().into_static(),
            (l, Null) => l.clone().into_static(),
            (Int32(l), Int32(r)) => Int32(l + r),
            (Int64(l), Int64(r)) => Int64(l + r),
            (UInt32(l), UInt32(r)) => UInt32(l + r),
            (UInt64(l), UInt64(r)) => UInt64(l + r),
            (Float16(l), Float16(r)) => Float16(*l + *r),
            (Float32(l), Float32(r)) => Float32(l + r),
            (Float64(l), Float64(r)) => Float64(l + r),
            #[cfg(feature = "dtype-duration")]
            (Duration(l, lu), Duration(r, ru)) => {
                if lu != ru {
                    unimplemented!("adding durations with different units is not supported here");
                }

                Duration(l + r, *lu)
            },
            #[cfg(feature = "dtype-decimal")]
            (Decimal(l, lp, ls), Decimal(r, rp, rs)) => {
                if (lp, ls) != (rp, rs) {
                    unimplemented!(
                        "adding decimals with different precisions/scales is not supported here"
                    );
                }

                Decimal(l + r, *lp, *ls)
            },
            _ => unimplemented!(),
        }
    }

View on GitHub (pinned to fe841f959e)