0

I've saved a simple numpy array by doing:

numpy.save(filepath, anarray)

I'm now trying to retrieve it using pickle (I don't want to switch to numpy.load because the code has to be flexible), but I get:

atuple = pickle.load(open(filepath, 'rb'))

_pickle.UnpicklingError: STACK_GLOBAL requires str

1 Answer 1

1

Numpy and pickle use different file formats. There's no reason to expect that you should be able to unpickle an array saved using np.save. If you need to be able to load things with pickle, you should save them with pickle.

Sign up to request clarification or add additional context in comments.

4 Comments

why does numpy.save have a default True allow_pickle option then ? also, numpy.load explicitly says that "Load arrays or pickled objects from .npy, .npz or pickled files." Does this mean that when dumping with pickle one can retrieve with numpy but not the opposite ?
If the array contains objects it can use pickle to serialize (and retrieve) them. Conversely pickle of an array uses the save to serialize it. So they are related. But it doesn't mean that files are wholely compatible.
@hpaulj I didn't know that pickle was using save as part of its implementation. That's cool! I think the point that this doesn't imply compatibility is important, though. You can save CSV inside of a JSON object, but that doesn't mean you'll be able to load it with a CSV reader.
@duff18 the allow_pickle option has to do with whether to allow saving or loading arrays with elements that are python objects (as opposed to eg numpy numeric types). Unpickling can execute arbitrary and potentially malicious code. allow_pickle=False is a guard against this, not a pickle compatibility setting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.