Fix np.load() ValueError: Object arrays cannot be loaded when allow_pickle=False – NumPy Tutorial

By | January 12, 2022

We often use numpy.load() function to load an object from a file. However we may get this error: ValueError: Object arrays cannot be loaded when allow_pickle=False. In this tutorial, we will introduce how to fix it.

Look at this example below:

import numpy as np

obj = np.load(file)

Here file is a npy file, which saved an object.

Run this code, we get this error:

Fix np.load() ValueError: Object arrays cannot be loaded when allow_pickle=False - NumPy Tutorial

How to fix this ValueError?

It is simple to fix this error. Here are two methods.

Method 1: install numpy 1.16.2 version.

Because numpy version > 1.16.2, all_pickle = False defaultly.

You can install numpy like:

pip install --force-reinstall numpy==1.16.2

Method 2: set allow_pickle=True when using np.load()

For example:

obj = np.load(file, allow_pickle=True)

Then you will find this error is fixed.

Leave a Reply

Your email address will not be published. Required fields are marked *