pola-rs/polars · error
should be hashable
Error message
should be hashable
What it means
The Hash impl for ObjectValue (wrapping an arbitrary Python object) in crates/polars-python/src/conversion/mod.rs calls the Python object's __hash__ and expects it to succeed. If the stored Python object is unhashable (e.g. list, dict, set), the `expect("should be hashable")` panics, raised from `hash` (called by `tot_hash`).
Solutions
- Only store hashable Python objects (or make the object hashable by defining __hash__ and __eq__)
- Avoid hashing operations on object columns; convert values to hashable representations (tuples instead of lists, immutable structs)
- Restructure the data into supported dtypes (Struct/List) instead of raw Python objects
Example fix
# before
pl.Series("cfg", [{"a": 1}, {"a": 2}]).n_unique() # dicts unhashable -> panic
# after
pl.Series("cfg", [(("a", 1),), (("a", 2),)]).n_unique() Defensive patterns
Strategy: validation
Validate before calling
if df.schema["col"] == pl.Object:
assert all(isinstance(v, collections.abc.Hashable) for v in df["col"].to_list()), "object column contains unhashable values; unique/group_by will panic" Type guard
import collections.abc
def all_hashable(values: list) -> bool:
return all(isinstance(v, collections.abc.Hashable) for v in values) Try / catch
try:
n = s.n_unique()
except pl.exceptions.PanicException as e:
if "should be hashable" in str(e):
n = len({repr(v) for v in s.to_list()}) # fallback on representation
else:
raise Prevention
- Only put hashable objects in Object-dtype columns if you plan to use unique/group_by/join
- Implement __hash__ and __eq__ on custom classes stored in object columns
- Prefer native dtypes (Struct/List) over raw Python objects
When it happens
Trigger: Putting unhashable Python objects into an Object-dtype Series/DataFrame and then triggering Rust-side hashing — e.g. using the Series in a unique/group_by/join/hash operation or hash-based dedup.
Common situations: Storing lists/dicts as 'object' column values and then calling .unique(), .n_unique(), group_by on that column, or a join involving the object column.
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 object
- activate 'object' feature
- Expecting to be able to downcast into bytes from read…
- python function failed
- python function should return List[str]
AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18).
Data as JSON: /api/errors/c2c5fbcc8f19fa5b.
Report an issue: GitHub.
Appendix: source
Thrown at crates/polars-python/src/conversion/mod.rs:811
}
#[derive(Debug)]
#[repr(transparent)]
pub struct ObjectValue {
pub inner: Py<PyAny>,
}
impl Clone for ObjectValue {
fn clone(&self) -> Self {
Python::attach(|py| Self {
inner: self.inner.clone_ref(py),
})
}
}
impl Hash for ObjectValue {
fn hash<H: Hasher>(&self, state: &mut H) {
let h = Python::attach(|py| self.inner.bind(py).hash().expect("should be hashable"));
state.write_isize(h)
}
}
impl Eq for ObjectValue {}
impl PartialEq for ObjectValue {
fn eq(&self, other: &Self) -> bool {
Python::attach(|py| {
match self
.inner
.bind(py)
.rich_compare(other.inner.bind(py), CompareOp::Eq)
{
Ok(result) => result.is_truthy().unwrap(),
Err(_) => false,
}
})View on GitHub (pinned to fe841f959e)