An array of objects is just a regular numpy array, where dtype
is an object
; this happens if the contents of the array are not ordinary numeric types (for example, int
or float
, etc.). We can try to save a numpy array with objects, just to test how it works. A simple view of an object would be a dict
:
>>> import numpy as np >>> a = np.array([{x: 1} for x in range(4)]) >>> a array([{0: 1}, {1: 1}, {2: 1}, {3: 1}], dtype=object) >>> np.save('test.pkl', a)
Downloading this function works fine:
>>> np.load('test.pkl.npy') array([{0: 1}, {1: 1}, {2: 1}, {3: 1}], dtype=object)
An array cannot be stored without using a brine:
>>> np.save('test.pkl', a, allow_pickle=False) ... ValueError: Object arrays cannot be saved when allow_pickle=False
The rule of thumb for pickles is that you are safe if you load pickled cucumbers, but you must be careful when loading pickles that you got from another place. Firstly, if you do not have the same libraries (or library versions) that were used to prepare the brine, you may not be able to load the brine (this is what is meant with portability above). Security is another potential concern; you can read a little about how pickles can be abused in this article , for example.
source share