Object arrays cannot be loaded when allow_pickle=False

avatar
Borislav Hadzhiev

Last updated: Jun 30, 2023
4 min

banner

# Object arrays cannot be loaded when allow_pickle=False

The Python "ValueError: Object arrays cannot be loaded when allow_pickle=False " occurs when you try to load an object array with the allow_pickle argument set to False.

To solve the error, set the allow_pickle argument to True or change the type of the array.

Here is an example of how the error occurs.

main.py
import numpy as np np.save('data.npy', np.array([[1, 2, 3], [4, 5, 6]], dtype=object)) arr = np.load('data.npy', allow_pickle=False) # ⛔️ ValueError: Object arrays cannot be loaded when allow_pickle=False print(arr)

valueerror object arrays cannot be loaded when allow pickle false

We used the numpy.save method to save an array to a binary file in NumPy (.npy) format.

The first argument the method takes is the file path.

The second argument is the array that should be saved and the third is the data type.

Notice that we set the dtype to object.

The numpy.load method loads arrays or pickled objects from .npy, .npz or pickled files.

The first argument the method takes is the file path.

The allow_pickle keyword argument determines whether it is allowed to load pickled object arrays that are stored in .npy files.

If the allow_pickle argument is set to False, loading object arrays causes the error.

By default, the allow_pickle argument is set to False.

# Set the allow_pickle argument to True to solve the error

One way to solve the error is to set the allow_pickle argument to True.

main.py
import numpy as np np.save('data.npy', np.array([[1, 2, 3], [4, 5, 6]], dtype=object)) arr = np.load('data.npy', allow_pickle=True) # [[1 2 3] # [4 5 6]] print(arr)

set allow pickle to true to solve error

When the allow_pickle keyword argument is set to True, loading object arrays is allowed.

Note that the default value of the allow_pickle argument is False, so it has to be explicitly specified.

The allow_pickle argument can also be passed to the numpy.save() method.

main.py
import numpy as np np.save( 'data.npy', np.array([[1, 2, 3], [4, 5, 6]], dtype=object), allow_pickle=True ) arr = np.load('data.npy', allow_pickle=True) # [[1 2 3] # [4 5 6]] print(arr)

However, passing the allow_pickle argument to numpy.save() is not necessary as it defaults to True.

You can also set the allow_pickle argument to True when using the with statement.

Replace this:

main.py
# ⛔️ Incorrect import numpy as np with np.load('data.npy') as arr: print(arr)

With this:

main.py
# ✅ Correct import numpy as np with np.load('data.npy', allow_pickle=True) as arr: print(arr)

# Setting a different data type for the NumPy array

You can also set a different data type for the NumPy array to solve the error.

For the error to occur, 2 conditions have to be met:

  1. The allow_pickle argument has to be set to False.
  2. The data type of the array has to be set to object.

You can set the data type of the array to int or float to solve the error.

main.py
import numpy as np np.save( 'data.npy', np.array([[1, 2, 3], [4, 5, 6]], dtype=int), ) arr = np.load('data.npy') # [[1 2 3] # [4 5 6]] print(arr)

set data type of array to not be object

We set the dtype (data type) of the array to int instead of object.

Notice that we didn't have to explicitly set the allow_pickle keyword argument.

# Try to downgrade numpy to version 1.16.1

If you use keras, you can also try to downgrade your version of the numpy module to 1.16.1 as the error occurs in numpy version 1.16.4 and more recent.

Open your terminal in your project's root directory and run the following command.

shell
pip install numpy==1.16.1 # or with pip3 pip3 install numpy==1.16.1 # or in Jupyter notebook !pip install numpy==1.16.1 # or with Anaconda conda install numpy==1.16.1

If you have a requirements.txt file, you can add the following line.

requirements.txt
numpy==1.16.1

# Solving the error when using keras

If you got the error when using the load_data() method in keras, try to set the default value of the allow_pickle keyword argument to True.

main.py
import numpy as np from tensorflow import keras np.load.__defaults__ = (None, True, True, 'ASCII') np.save('data.npy', np.array([[1, 2, 3], [4, 5, 6]], dtype=object)) (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() np.load.__defaults__ = (None, False, True, 'ASCII')

We updated the __defaults__ tuple of the numpy.load() method, setting allow_pickle to True.

allow_pickle is the second argument in the tuple.

Once we're done, we set the default value of the argument back to False.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.