{"record":{"id":"a12b5c38515871b1","repo":"pola-rs/polars","slug":"comparing-durations-with-different-units-is-not-su","errorCode":null,"errorMessage":"comparing durations with different units is not supported","messagePattern":"comparing durations with different units is not supported","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":1437,"sourceCode":"            (Float64(l), Float64(r)) => Some(l.tot_cmp(r)),\n            (String(l), String(r)) => l.partial_cmp(r),\n            (Binary(l), Binary(r)) => l.partial_cmp(r),\n            #[cfg(feature = \"dtype-date\")]\n            (Date(l), Date(r)) => l.partial_cmp(r),\n            #[cfg(feature = \"dtype-datetime\")]\n            (Datetime(lt, lu, lz), Datetime(rt, ru, rz)) => {\n                if lu != ru || lz != rz {\n                    unimplemented!(\n                        \"comparing datetimes with different units or timezones is not supported\"\n                    );\n                }\n\n                lt.partial_cmp(rt)\n            },\n            #[cfg(feature = \"dtype-duration\")]\n            (Duration(lt, lu), Duration(rt, ru)) => {\n                if lu != ru {\n                    unimplemented!(\"comparing durations with different units is not supported\");\n                }\n\n                lt.partial_cmp(rt)\n            },\n            #[cfg(feature = \"dtype-time\")]\n            (Time(l), Time(r)) => l.partial_cmp(r),\n            #[cfg(feature = \"dtype-categorical\")]\n            (Categorical(l_cat, l_map), Categorical(r_cat, r_map)) => unsafe {\n                let l_str = l_map.cat_to_str_unchecked(*l_cat);\n                let r_str = r_map.cat_to_str_unchecked(*r_cat);\n                l_str.partial_cmp(r_str)\n            },\n            #[cfg(feature = \"dtype-categorical\")]\n            (Enum(l_cat, l_map), Enum(r_cat, r_map)) => {\n                if !Arc::ptr_eq(l_map, r_map) {\n                    unimplemented!(\"can't order enums from different FrozenCategories\")\n                }\n                l_cat.partial_cmp(r_cat)","sourceCodeStart":1419,"sourceCodeEnd":1455,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L1419-L1455","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Cast both duration columns to the same TimeUnit before comparing (`.cast(&DataType::Duration(TimeUnit::Nanoseconds, None))`)","Use polars expression-level comparisons which normalize units, rather than raw AnyValue::partial_cmp","Standardize the time unit at parse/creation time so all duration columns in the workflow use one unit","Convert durations to a fixed unit via `.dt.cast_time_unit(...)` in expressions"],"exampleFix":"// before\nlet d1 = Series::new(\"d1\", &[1i64]).unwrap().cast(&DataType::Duration(TimeUnit::Milliseconds, None)).unwrap();\nlet d2 = Series::new(\"d2\", &[1000i64]).unwrap().cast(&DataType::Duration(TimeUnit::Nanoseconds, None)).unwrap();\n// comparing AnyValues of d1 vs d2 panics\n// after\nlet d2 = d2.cast(&DataType::Duration(TimeUnit::Milliseconds, None)).unwrap();\nlet _ = d1.equal(&d2)?;","handlingStrategy":"validation","validationCode":"fn durations_comparable(a: &AnyValue, b: &AnyValue) -> bool {\n    match (a, b) {\n        (AnyValue::Duration(_, lu), AnyValue::Duration(_, ru)) => lu == ru,\n        _ => false,\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fix one duration TimeUnit for the whole pipeline and cast on ingest","Check dtype metadata (unit) equality before comparing duration values","Use expression comparisons (dt.cast_time_unit) rather than AnyValue::partial_cmp","Watch Arrow/Parquet imports that may carry different duration units"],"tags":["polars","duration","comparison","time-unit"],"backgroundTag":"dtype-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}