pola-rs/polars · error
` ` operation not supported for dtype
Error message
`{}` operation not supported for dtype `{}` What it means
polars-core's Series defines an `invalid_operation_panic!` macro used throughout series/method implementations: when an operation (add, sub, eq, rem, etc.) is applied to a dtype that does not implement it, the code panics with '`<op>` operation not supported for dtype `<dtype>`'. It is the catch-all arm of dtype-dispatched operators in crates/polars-core/src/series/mod.rs and sibling impls.
Solutions
- Check `series.dtype()` and cast to a compatible numeric dtype before applying the operator.
- For booleans, use logical ops (&, |) or cast to UInt8 for arithmetic.
- For strings used numerically, apply `cast(DataType::Int64)`/`str.to_integer()` first.
- Skip or handle non-numeric columns explicitly in generic per-column loops.
Example fix
// before let total = &bool_col_a + &bool_col_b; // panics // after let total = &bool_col_a.cast(&DataType::UInt8)? + &bool_col_b.cast(&DataType::UInt8)?;
Defensive patterns
Strategy: type-guard
Validate before calling
fn is_numeric(s: &Series) -> bool {
s.dtype().is_numeric()
}
if !is_numeric(&a) || !is_numeric(&b) { /* skip or cast */ } Type guard
fn numeric_pair(a: &Series, b: &Series) -> Option<(Series, Series)> {
(a.dtype().is_numeric() && b.dtype().is_numeric())
.then(|| (a.clone(), b.clone()))
} Prevention
- Guard operators with dtype checks in generic per-column code.
- Cast bools to numeric and strings to integers before arithmetic.
- Inspect `df.schema()` after reading external data before computing.
When it happens
Trigger: Applying arithmetic/comparison operators on Series with unsupported dtypes, e.g. `bool_series + bool_series`, subtracting two String Series, bitwise/remainder ops on temporal or nested dtypes; dispatching generic operations across all DataFrame columns where some column dtype lacks the operator impl.
Common situations: Generic code summing every column of a DataFrame; boolean columns mistakenly treated as 0/1 numerics; String columns loaded from CSV being used in arithmetic before a `str.to_integer()`/cast.
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
- activate dtype
- activate feature dtype-duration
- activate 'round_series' feature
- cannot create duration from other type. dtype
- cannot create time from other type. dtype
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/30593beaa95d4f8b.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-core/src/series/mod.rs:10
#![allow(unsafe_op_in_unsafe_fn)]
//! Type agnostic columnar data structure.
use crate::chunked_array::flags::StatisticsFlags;
pub use crate::prelude::ChunkCompareEq;
use crate::prelude::*;
use crate::{HEAD_DEFAULT_LENGTH, TAIL_DEFAULT_LENGTH};
macro_rules! invalid_operation_panic {
($op:ident, $s:expr) => {
panic!(
"`{}` operation not supported for dtype `{}`",
stringify!($op),
$s._dtype()
)
};
}
pub mod amortized_iter;
mod any_value;
pub mod arithmetic;
pub mod arrow_export;
pub mod builder;
mod comparison;
mod from;
pub mod implementations;
pub(crate) mod iterator;
pub mod ops;View on GitHub (pinned to fe841f959e)