Last modified: Apr 24, 2026 By Alexander Williams

Fix TypeError Unhashable Type ndarray

If you work with NumPy arrays in Python, you might see this error: TypeError: unhashable type: 'numpy.ndarray'. It happens when you try to use a NumPy array where a hashable object is required, like a dictionary key or a set element.

This error is common for beginners. It stops your code and can be confusing. But the fix is simple once you understand why it happens. This article explains the cause, shows examples, and gives you clear solutions.

What Does "Unhashable Type" Mean?

In Python, hashable objects have a fixed hash value. Integers, strings, and tuples are hashable. They can be used as dictionary keys or added to sets.

Unhashable objects, like lists and NumPy arrays, can change their content. Their hash value could change, so Python forbids using them in places that need a fixed hash. NumPy arrays are mutable, so they are unhashable.

When you try to use a NumPy array as a key or in a set, Python raises TypeError: unhashable type: 'numpy.ndarray'.

Common Scenarios That Trigger This Error

Here are the three most common situations where this error appears:

1. Using a NumPy Array as a Dictionary Key

Dictionaries need hashable keys. If you try to use a NumPy array, Python will complain.

 
import numpy as np

# Example that causes the error
arr = np.array([1, 2, 3])
my_dict = {arr: "value"}  # This line raises TypeError

TypeError: unhashable type: 'numpy.ndarray'

To fix this, convert the array to a hashable type. Use tuple() to turn it into a tuple.

 
import numpy as np

arr = np.array([1, 2, 3])
# Convert to tuple before using as key
key = tuple(arr)
my_dict = {key: "value"}
print(my_dict)  # Output: {(1, 2, 3): 'value'}

2. Adding a NumPy Array to a Set

Sets only store hashable items. You cannot add a NumPy array directly.

 
import numpy as np

arr = np.array([4, 5, 6])
my_set = set()
my_set.add(arr)  # This raises TypeError

TypeError: unhashable type: 'numpy.ndarray'

The solution is to convert the array to a tuple before adding it to the set.

 
import numpy as np

arr = np.array([4, 5, 6])
my_set = set()
my_set.add(tuple(arr))
print(my_set)  # Output: {(4, 5, 6)}

3. Using a NumPy Array in a Pandas Operation

Sometimes this error appears when working with Pandas DataFrames. For example, when you try to use a NumPy array as an index or column label.

 
import numpy as np
import pandas as pd

arr = np.array([10, 20, 30])
df = pd.DataFrame({'A': [1, 2, 3]})
# Trying to use array as index label
df.loc[arr]  # This raises TypeError

To fix it, convert the array to a list or tuple. Also check if you meant to use array values as index positions.

 
import numpy as np
import pandas as pd

arr = np.array([10, 20, 30])
df = pd.DataFrame({'A': [1, 2, 3]}, index=[10, 20, 30])
# Convert to list for indexing
df.loc[arr.tolist()]  # Works correctly

Why NumPy Arrays Are Unhashable

NumPy arrays are mutable. You can change their values after creation. Hashable objects must be immutable. If a dictionary key could change, the dictionary would break.

Python uses hash values to find dictionary keys quickly. If the hash changes, the key becomes lost. That is why Python only allows immutable types as keys.

Tuples are immutable and hashable. NumPy arrays are not. So you must convert arrays to tuples or strings before using them as keys.

How to Fix the Error: Step-by-Step

Follow these steps when you see TypeError: unhashable type: 'numpy.ndarray':

  1. Identify where you use the array as a dictionary key, set element, or in a similar context.
  2. Convert the array to a hashable type. Use tuple(array) for simple arrays.
  3. For 2D arrays, convert each row to a tuple of tuples: tuple(map(tuple, array)).
  4. Test your code again. The error should disappear.

Here is a complete example for a 2D array:

 
import numpy as np

arr_2d = np.array([[1, 2], [3, 4]])
# Convert to tuple of tuples
hashable_key = tuple(map(tuple, arr_2d))
my_dict = {hashable_key: "2D array value"}
print(my_dict)  # Output: {((1, 2), (3, 4)): '2D array value'}

Alternative Solutions

Sometimes you do not need to store the array itself. You can store its string representation or a hash of its content.

Use str(array) to get a string key:

 
import numpy as np

arr = np.array([7, 8, 9])
key = str(arr)
my_dict = {key: "string key"}
print(my_dict)  # Output: {'[7 8 9]': 'string key'}

Use hashlib for a fixed-length hash if you need to compare arrays:

 
import numpy as np
import hashlib

arr = np.array([1, 2, 3])
hash_key = hashlib.sha256(arr.tobytes()).hexdigest()
my_dict = {hash_key: "hash key"}

For more general Python errors, see our guide on Python TypeError: Causes and Fixes.

Preventing the Error in Your Code

To avoid this error in the future, follow these best practices:

  • Always convert arrays to tuples or strings before using them as dictionary keys.
  • Use lists instead of arrays when you need hashable data. Lists are also unhashable, but you can convert them to tuples.
  • Check your function arguments. If a function expects a hashable input, do not pass a NumPy array.
  • Use isinstance() to check types before using them as keys.
 
import numpy as np

def safe_key(value):
    if isinstance(value, np.ndarray):
        return tuple(value)
    return value

arr = np.array([5, 6, 7])
key = safe_key(arr)
my_dict = {key: "safe"}
print(my_dict)  # Output: {(5, 6, 7): 'safe'}

Conclusion

The TypeError: unhashable type: 'numpy.ndarray' error is easy to fix. It happens because NumPy arrays are mutable and cannot be used as dictionary keys or set elements. Convert the array to a tuple, string, or hash before using it. Always check your data types when working with dictionaries and sets. With these solutions, you can solve this error quickly and keep your code running smoothly.

If you encounter other type errors, check out our article on Python TypeError: Causes and Fixes for more help.