pandas TypeError: unhashable type: 'numpy.ndarray' (tested)
TypeError: unhashable type: 'numpy.ndarray' means a NumPy array reached something that needs a hash: a column of arrays in drop_duplicates, groupby or nunique, or an array used as a dict key. Convert to tuples with tuple(a.tolist()). Tested on pandas 2.3.3 and 3.1.0.
1. Where it comes from
import numpy as np, pandas as pd
df = pd.DataFrame({"id": [1, 2, 3], "vec": [np.array([1, 2]), np.array([3]), np.array([1, 2])]})
for name, call in [("drop_duplicates", lambda: df.drop_duplicates()),
("groupby", lambda: df.groupby("vec").size()),
("nunique", lambda: df["vec"].nunique()),
("dict key", lambda: {df["vec"][0]: 1})]:
try:
call()
print(name, "OK")
except TypeError as e:
print(name, "-> TypeError:", e)
Output, identical on pandas 2.3.3 and 3.1.0 (Python 3.14.7):
drop_duplicates -> TypeError: unhashable type: 'numpy.ndarray'
groupby -> TypeError: unhashable type: 'numpy.ndarray'
nunique -> TypeError: unhashable type: 'numpy.ndarray'
dict key -> TypeError: cannot use 'numpy.ndarray' as a dict key (unhashable type: 'numpy.ndarray')
A NumPy array is mutable, so it has no hash. Any pandas operation that hashes cells (drop_duplicates, groupby, nunique) fails when a column holds arrays, typically embeddings, vectors or the output of apply returning np.array. Using an array as a dict key fails too; Python 3.14 words that case as "cannot use 'numpy.ndarray' as a dict key".
2. Fix: a tuple column for hashing
import numpy as np, pandas as pd
df = pd.DataFrame({"id": [1, 2, 3], "vec": [np.array([1, 2]), np.array([3]), np.array([1, 2])]})
df["vec_t"] = df["vec"].apply(lambda a: tuple(a.tolist()))
print(df.drop_duplicates(subset="vec_t")[["id", "vec_t"]])
print(df.groupby("vec_t").size().to_dict())
Output, identical on pandas 2.3.3 and 3.1.0 (Python 3.14.7):
id vec_t
0 1 (1, 2)
1 2 (3,)
{(1, 2): 2, (3,): 1}
tuple(a.tolist()) turns each array into a hashable tuple of plain Python numbers; deduplicate or group on that column and keep the array column for computation. If you meant to look up a single value, index the array first (arr[0] or arr.item()) instead of using the whole array as a key.
Related: pandas unhashable type: 'list' · unhashable type: 'Series' · _ARRAY_API not found (NumPy 2).
Frequently asked questions
Why is numpy.ndarray unhashable?
Arrays are mutable, so NumPy does not define a hash for them; dict keys, sets and pandas hashing operations need one.
How do I drop duplicates on a column of NumPy arrays?
Add a tuple column with df['col'].apply(lambda a: tuple(a.tolist())) and pass it as subset= to drop_duplicates.
Does pandas 3 change this error?
No. drop_duplicates, groupby and nunique raised the same message on pandas 2.3.3 and 3.1.0.
Written with AI assistance. Measured on 2026-10-23 in the python:3-alpine Docker image (Python 3.14.7) with pandas 2.3.3 and pandas 3.1.0. Outputs pasted unedited. PlainTables is our extension; Python, NumPy and pandas are free.