@@ -2908,56 +2908,63 @@ any pickled pandas object (or any other pickled object) from file:
2908
2908
import os
2909
2909
os.remove(' foo.pkl' )
2910
2910
2911
- The ``to_pickle `` and ``read_pickle `` methods can read and write compressed pickle files.
2912
- For ``read_pickle `` method, ``compression `` parameter can be one of
2913
- {``'infer' ``, ``'gzip' ``, ``'bz2' ``, ``'zip' ``, ``'xz' ``, ``None ``}, default ``'infer' ``.
2914
- If 'infer', then use gzip, bz2, zip, or xz if filename ends in '.gz', '.bz2', '.zip', or
2915
- '.xz', respectively. If using 'zip', the ZIP file must contain only one data file to be
2916
- read in. Set to ``None `` for no decompression.
2917
- ``to_pickle `` works in a similar way, except that 'zip' format is not supported. If the
2918
- filename ends with '.zip', an exception will be raised.
2911
+ .. warning ::
2912
+
2913
+ Loading pickled data received from untrusted sources can be unsafe.
2914
+
2915
+ See: http://docs.python.org/2.7/library/pickle.html
2916
+
2917
+ .. warning ::
2918
+
2919
+ Several internal refactorings, 0.13 (:ref: `Series Refactoring <whatsnew_0130.refactoring >`), and 0.15 (:ref: `Index Refactoring <whatsnew_0150.refactoring >`),
2920
+ preserve compatibility with pickles created prior to these versions. However, these must
2921
+ be read with ``pd.read_pickle ``, rather than the default python ``pickle.load ``.
2922
+ See `this question <http://stackoverflow.com/questions/20444593/pandas-compiled-from-source-default-pickle-behavior-changed >`__
2923
+ for a detailed explanation.
2924
+
2925
+ .. note ::
2926
+
2927
+ These methods were previously ``pd.save `` and ``pd.load ``, prior to 0.12.0, and are now deprecated.
2928
+
2929
+ .. _io.pickle.compression :
2930
+
2931
+ Read/Write compressed pickle files
2932
+ ''''''''''''''
2933
+
2934
+ .. versionadded :: 0.20.0
2919
2935
2920
- .. versionadded :: 0.20.0
2936
+ :func: `read_pickle `, :meth: `DataFame.to_pickle ` and :meth: `Series.to_pickle ` can read
2937
+ and write compressed pickle files. Compression types of ``gzip ``, ``bz2 ``, ``xz `` supports
2938
+ both read and write. ``zip `` file supports read only and must contain only one data file
2939
+ to be read in.
2940
+ Compression type can be an explicitely parameter or be inferred from the file extension.
2941
+ If 'infer', then use ``gzip ``, ``bz2 ``, ``zip ``, or ``xz `` if filename ends in ``'.gz' ``, ``'.bz2' ``, ``'.zip' ``, or
2942
+ ``'.xz' ``, respectively.
2921
2943
2922
2944
.. ipython :: python
2923
2945
2924
2946
df = pd.DataFrame({
2925
2947
' A' : np.random.randn(1000 ),
2926
2948
' B' : np.random.randn(1000 ),
2927
2949
' C' : np.random.randn(1000 )})
2928
- df.to_pickle(" data.pkl.xz" )
2929
- df.to_pickle(" data.pkl.compress" , compression = " gzip" )
2950
+ df.to_pickle(" data.pkl.compress" , compression = " gzip" ) # explicit compression type
2951
+ df.to_pickle(" data.pkl.xz" , compression = " infer" ) # infer compression type from extension
2952
+ df.to_pickle(" data.pkl.gz" ) # default, using "infer"
2930
2953
df[" A" ].to_pickle(" s1.pkl.bz2" )
2931
2954
2932
- df = pd.read_pickle(" data.pkl.xz" )
2933
2955
df = pd.read_pickle(" data.pkl.compress" , compression = " gzip" )
2956
+ df = pd.read_pickle(" data.pkl.xz" , compression = " infer" )
2957
+ df = pd.read_pickle(" data.pkl.gz" )
2934
2958
s = pd.read_pickle(" s1.pkl.bz2" )
2935
2959
2936
2960
.. ipython :: python
2937
2961
:suppress:
2938
2962
import os
2939
- os.remove(" data.pkl.xz" )
2940
2963
os.remove(" data.pkl.compress" )
2964
+ os.remove(" data.pkl.xz" )
2965
+ os.remove(" data.pkl.gz" )
2941
2966
os.remove(" s1.pkl.bz2" )
2942
2967
2943
- .. warning ::
2944
-
2945
- Loading pickled data received from untrusted sources can be unsafe.
2946
-
2947
- See: http://docs.python.org/2.7/library/pickle.html
2948
-
2949
- .. warning ::
2950
-
2951
- Several internal refactorings, 0.13 (:ref: `Series Refactoring <whatsnew_0130.refactoring >`), and 0.15 (:ref: `Index Refactoring <whatsnew_0150.refactoring >`),
2952
- preserve compatibility with pickles created prior to these versions. However, these must
2953
- be read with ``pd.read_pickle ``, rather than the default python ``pickle.load ``.
2954
- See `this question <http://stackoverflow.com/questions/20444593/pandas-compiled-from-source-default-pickle-behavior-changed >`__
2955
- for a detailed explanation.
2956
-
2957
- .. note ::
2958
-
2959
- These methods were previously ``pd.save `` and ``pd.load ``, prior to 0.12.0, and are now deprecated.
2960
-
2961
2968
.. _io.msgpack :
2962
2969
2963
2970
msgpack
0 commit comments