pola-rs/polars · error

comparing datetimes with different units or timezones is…

Error message

comparing datetimes with different units or timezones is not supported

What it means

AnyValue::partial_cmp only supports comparing two Datetime values when they have identical TimeUnit and TimeZone. Comparing e.g. a millisecond datetime with a nanosecond one, or a UTC datetime with a zoned one, is rejected with unimplemented!() because raw integer comparison across units/zones would be wrong.

Solutions

  1. Cast both datetime columns to the same time unit (e.g. TimeUnit::Microseconds) and the same time zone before comparing
  2. Use polars expressions (col comparison operators) instead of raw AnyValue comparison; they handle unit conversion
  3. Convert one side with `.dt.convert_time_zone(...)` / `.dt.cast_time_unit(...)` before the comparison
  4. Normalize parsing: always specify explicit time_unit and time_zone in to_datetime so columns match

Example fix

// before: comparing values from columns with different units
let a = df.column("ts_ms")?.as_materialized_series(); // Datetime(ms, UTC)
let b = df.column("ts_us")?.as_materialized_series(); // Datetime(us, UTC)
// comparing AnyValues from a and b panics
// after: unify unit and timezone first
let b = b.cast(&DataType::Datetime(TimeUnit::Milliseconds, Some(PlSmallStr::from_static("UTC"))))?;
let eq = a.equal(&b)?;
Defensive patterns

Strategy: validation

Validate before calling

fn datetimes_comparable(a: &AnyValue, b: &AnyValue) -> bool {
    match (a, b) {
        (AnyValue::Datetime(_, lu, lz), AnyValue::Datetime(_, ru, rz)) => lu == ru && lz == rz,
        _ => false,
    }
}

Prevention

When it happens

Trigger: Calling AnyValue::partial_cmp on two AnyValue::Datetime values where the TimeUnit (lu != ru) or the TimeZone string (lz != rz) differs — typically by extracting values from two datetime columns with different time units or timezone settings and comparing them directly.

Common situations: Comparing a ms-precision column with a us/ns-precision column, comparing tz-aware vs naive datetimes, or mixing datetimes parsed with different `.str.to_datetime(time_unit=..., time_zone=...)` settings in filters/sorts over extracted AnyValues.

Related errors


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

Appendix: source

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

            (UInt32(l), UInt32(r)) => l.partial_cmp(r),
            (UInt64(l), UInt64(r)) => l.partial_cmp(r),
            (UInt128(l), UInt128(r)) => l.partial_cmp(r),
            (Int8(l), Int8(r)) => l.partial_cmp(r),
            (Int16(l), Int16(r)) => l.partial_cmp(r),
            (Int32(l), Int32(r)) => l.partial_cmp(r),
            (Int64(l), Int64(r)) => l.partial_cmp(r),
            (Int128(l), Int128(r)) => l.partial_cmp(r),
            (Float16(l), Float16(r)) => Some(l.tot_cmp(r)),
            (Float32(l), Float32(r)) => Some(l.tot_cmp(r)),
            (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 {

View on GitHub (pinned to fe841f959e)