votes up 2

Object arrays cannot be loaded when allow_pickle=False

Package:
numpy
github stars 18118
Exception Class:
ValueError

Raise code

count = numpy.multiply.reduce(shape, dtype=numpy.int64)

    # Now read the actual data.
    if dtype.hasobject:
        # The array contained Python objects. We need to unpickle the data.
        if not allow_pickle:
            raise ValueError("Object arrays cannot be loaded when "
                             "allow_pickle=False")
        if pickle_kwargs is None:
            pickle_kwargs = {}
        try:
            array = pickle.load(fp, **pickle_kwargs)
        except UnicodeError as err:
            # Friendlier error message
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 3 votes down

Numpy.savez saves several arrays into a single file in uncompressed .npz format.

Error code:

import numpy as np 

# Array
a=np.array([[123], [456]])
# Object array
b={'data':'somet',
   'data_2':'defin'}
#Save arrays into file
np.savez('/content/123.npz', a=a, b=b)
#Load file into data variable
data = np.load('/content/123.npz')
print(data['b'])

But when we have an object array or Dictionary, we are getting an error.

To fix it, just use allow_pickle = True.

import numpy as np 

a=np.array([[123], [456]])
# Object array
b={'data':'somet',
   'data_2':'defin'}
#Save arrays into file
np.savez('/content/123.npz', a=a, b=b)
#load file into data variable
data = np.load('/content/123.npz',allow_pickle=True)
print(data['b'])
Jul 01, 2021 anonim answer
anonim 13.0k

Add a possible fix

Please authorize to post fix