{"record":{"id":"d8b45ee7da26ac50","repo":"pola-rs/polars","slug":"adding-durations-with-different-units-is-not-suppo","errorCode":null,"errorMessage":"adding durations with different units is not supported here","messagePattern":"adding durations with different units is not supported here","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/datatypes/any_value.rs","lineNumber":1046,"sourceCode":"    }\n\n    #[must_use]\n    pub fn add(&self, rhs: &AnyValue) -> AnyValue<'static> {\n        use AnyValue::*;\n        match (self, rhs) {\n            (Null, r) => r.clone().into_static(),\n            (l, Null) => l.clone().into_static(),\n            (Int32(l), Int32(r)) => Int32(l + r),\n            (Int64(l), Int64(r)) => Int64(l + r),\n            (UInt32(l), UInt32(r)) => UInt32(l + r),\n            (UInt64(l), UInt64(r)) => UInt64(l + r),\n            (Float16(l), Float16(r)) => Float16(*l + *r),\n            (Float32(l), Float32(r)) => Float32(l + r),\n            (Float64(l), Float64(r)) => Float64(l + r),\n            #[cfg(feature = \"dtype-duration\")]\n            (Duration(l, lu), Duration(r, ru)) => {\n                if lu != ru {\n                    unimplemented!(\"adding durations with different units is not supported here\");\n                }\n\n                Duration(l + r, *lu)\n            },\n            #[cfg(feature = \"dtype-decimal\")]\n            (Decimal(l, lp, ls), Decimal(r, rp, rs)) => {\n                if (lp, ls) != (rp, rs) {\n                    unimplemented!(\n                        \"adding decimals with different precisions/scales is not supported here\"\n                    );\n                }\n\n                Decimal(l + r, *lp, *ls)\n            },\n            _ => unimplemented!(),\n        }\n    }\n","sourceCodeStart":1028,"sourceCodeEnd":1064,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-core/src/datatypes/any_value.rs#L1028-L1064","documentation":"`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.","triggerScenarios":"Using the `+` operator (Add impl) on two AnyValue::Duration values with different TimeUnits — e.g. `Duration(1, Ms) + Duration(1, Ns)`.","commonSituations":"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.","solutions":["Normalize both durations to the same TimeUnit (cast one column with `.cast(DataType::Duration(other_tu))`) before adding","Align units at ingestion time so all duration columns use a single TimeUnit","Perform the addition on the Series/ChunkedArray level where unit conversion is handled"],"exampleFix":"// before\nlet sum = av_ms.add(&av_ns); // unimplemented! panic\n// after\nlet av_ns_as_ms = AnyValue::Duration(ns_value / 1_000_000, TimeUnit::Milliseconds);\nlet sum = av_ms.add(&av_ns_as_ms);","handlingStrategy":"validation","validationCode":"fn can_add_durations(l: &AnyValue, r: &AnyValue) -> bool {\n    match (l, r) {\n        (AnyValue::Duration(_, lu), AnyValue::Duration(_, ru)) => lu == ru,\n        _ => true,\n    }\n}","typeGuard":"fn same_time_unit(l: &AnyValue, r: &AnyValue) -> bool {\n    matches!((l, r), (AnyValue::Duration(_, lu), AnyValue::Duration(_, ru)) if lu == ru)\n}","tryCatchPattern":null,"preventionTips":["Standardize one TimeUnit across all duration columns","Cast operands to a common unit before arithmetic","Prefer Series-level arithmetic over AnyValue-level ops"],"tags":["rust","polars","duration","arithmetic"],"backgroundTag":"unsupported-operation","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"}