10.7 Pickling
All pandas objects are equipped with to_pickle
methods which use Python’s
cPickle
module to save data structures to disk using the pickle format.
In [1]: df
Out[1]:
0 1 2
0 0.4691 -0.2829 -1.5091
1 -1.1356 1.2121 -0.1732
2 0.1192 -1.0442 -0.8618
3 -2.1046 -0.4949 1.0718
4 0.7216 -0.7068 -1.0396
In [2]: df.to_pickle('foo.pkl')
The read_pickle
function in the pandas
namespace can be used to load
any pickled pandas object (or any other pickled object) from file:
In [3]: pd.read_pickle('foo.pkl')
Out[3]:
0 1 2
0 0.4691 -0.2829 -1.5091
1 -1.1356 1.2121 -0.1732
2 0.1192 -1.0442 -0.8618
3 -2.1046 -0.4949 1.0718
4 0.7216 -0.7068 -1.0396
Warning
Loading pickled data received from untrusted sources can be unsafe.
Warning
Several internal refactorings, 0.13 (Series Refactoring), and 0.15 (Index Refactoring),
preserve compatibility with pickles created prior to these versions. However, these must
be read with pd.read_pickle
, rather than the default python pickle.load
.
See this question
for a detailed explanation.
Note
These methods were previously pd.save
and pd.load
, prior to 0.12.0, and are now deprecated.