Skip to content

Commit 4e02cae

Browse files
committed
Merge pull request #3787 from hayd/read_pickle_and_to_pickle
CLN deprecate save&load in favour of to_pickle&read_pickle
2 parents bd4b69f + 2011da2 commit 4e02cae

File tree

15 files changed

+168
-116
lines changed

15 files changed

+168
-116
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ pandas 0.11.1
137137
- removed ``Excel`` support to ``pandas.io.excel``
138138
- added top-level ``pd.read_sql`` and ``to_sql`` DataFrame methods
139139
- removed ``clipboard`` support to ``pandas.io.clipboard``
140+
- replace top-level and instance methods ``save`` and ``load`` with top-level ``read_pickle`` and
141+
``to_pickle`` instance method, ``save`` and ``load`` will give deprecation warning.
140142
- the ``method`` and ``axis`` arguments of ``DataFrame.replace()`` are
141143
deprecated
142144
- Implement ``__nonzero__`` for ``NDFrame`` objects (GH3691_, GH3696_)

doc/source/api.rst

+5-9
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ Input/Output
1313
Pickling
1414
~~~~~~~~
1515

16-
.. currentmodule:: pandas.core.common
16+
.. currentmodule:: pandas.io.pickle
1717

1818
.. autosummary::
1919
:toctree: generated/
2020

21-
load
22-
save
21+
read_pickle
2322

2423
Flat File
2524
~~~~~~~~~
@@ -378,8 +377,7 @@ Serialization / IO / Conversion
378377
:toctree: generated/
379378

380379
Series.from_csv
381-
Series.load
382-
Series.save
380+
Series.to_pickle
383381
Series.to_csv
384382
Series.to_dict
385383
Series.to_sparse
@@ -601,8 +599,7 @@ Serialization / IO / Conversion
601599
DataFrame.from_items
602600
DataFrame.from_records
603601
DataFrame.info
604-
DataFrame.load
605-
DataFrame.save
602+
DataFrame.to_pickle
606603
DataFrame.to_csv
607604
DataFrame.to_hdf
608605
DataFrame.to_dict
@@ -770,8 +767,7 @@ Serialization / IO / Conversion
770767
:toctree: generated/
771768

772769
Panel.from_dict
773-
Panel.load
774-
Panel.save
770+
Panel.to_pickle
775771
Panel.to_excel
776772
Panel.to_sparse
777773
Panel.to_frame

doc/source/basics.rst

-40
Original file line numberDiff line numberDiff line change
@@ -1207,46 +1207,6 @@ While float dtypes are unchanged.
12071207
casted
12081208
casted.dtypes
12091209
1210-
.. _basics.serialize:
1211-
1212-
Pickling and serialization
1213-
--------------------------
1214-
1215-
All pandas objects are equipped with ``save`` methods which use Python's
1216-
``cPickle`` module to save data structures to disk using the pickle format.
1217-
1218-
.. ipython:: python
1219-
1220-
df
1221-
df.save('foo.pickle')
1222-
1223-
The ``load`` function in the ``pandas`` namespace can be used to load any
1224-
pickled pandas object (or any other pickled object) from file:
1225-
1226-
1227-
.. ipython:: python
1228-
1229-
load('foo.pickle')
1230-
1231-
There is also a ``save`` function which takes any object as its first argument:
1232-
1233-
.. ipython:: python
1234-
1235-
save(df, 'foo.pickle')
1236-
load('foo.pickle')
1237-
1238-
.. ipython:: python
1239-
:suppress:
1240-
1241-
import os
1242-
os.remove('foo.pickle')
1243-
1244-
.. warning::
1245-
1246-
Loading pickled data received from untrusted sources can be unsafe.
1247-
1248-
See: http://docs.python.org/2.7/library/pickle.html
1249-
12501210
12511211
Working with package options
12521212
----------------------------

doc/source/io.rst

+37
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ object.
3939
* ``read_html``
4040
* ``read_stata``
4141
* ``read_clipboard``
42+
* ``read_pickle``
4243

4344
The corresponding ``writer`` functions are object methods that are accessed like ``df.to_csv()``
4445

@@ -50,6 +51,7 @@ The corresponding ``writer`` functions are object methods that are accessed like
5051
* ``to_html``
5152
* ``to_stata``
5253
* ``to_clipboard``
54+
* ``to_pickle``
5355

5456
.. _io.read_csv_table:
5557

@@ -1442,7 +1444,42 @@ We can see that we got the same content back, which we had earlier written to th
14421444
You may need to install xclip or xsel (with gtk or PyQt4 modules) on Linux to use these methods.
14431445

14441446

1447+
.. _io.serialize:
14451448

1449+
Pickling and serialization
1450+
--------------------------
1451+
1452+
All pandas objects are equipped with ``to_pickle`` methods which use Python's
1453+
``cPickle`` module to save data structures to disk using the pickle format.
1454+
1455+
.. ipython:: python
1456+
1457+
df
1458+
df.to_pickle('foo.pkl')
1459+
1460+
The ``read_pickle`` function in the ``pandas`` namespace can be used to load
1461+
any pickled pandas object (or any other pickled object) from file:
1462+
1463+
1464+
.. ipython:: python
1465+
1466+
read_pickle('foo.pkl')
1467+
1468+
.. ipython:: python
1469+
:suppress:
1470+
1471+
import os
1472+
os.remove('foo.pkl')
1473+
1474+
.. warning::
1475+
1476+
Loading pickled data received from untrusted sources can be unsafe.
1477+
1478+
See: http://docs.python.org/2.7/library/pickle.html
1479+
1480+
.. note::
1481+
1482+
These methods were previously ``save`` and ``load``, now deprecated.
14461483

14471484
.. _io.excel:
14481485

pandas/core/api.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from pandas.core.algorithms import factorize, match, unique, value_counts
7-
from pandas.core.common import isnull, notnull, save, load
7+
from pandas.core.common import isnull, notnull
88
from pandas.core.categorical import Categorical, Factor
99
from pandas.core.format import (set_printoptions, reset_printoptions,
1010
set_eng_float_format)
@@ -28,6 +28,7 @@
2828

2929
# legacy
3030
from pandas.core.daterange import DateRange # deprecated
31+
from pandas.core.common import save, load # deprecated, remove in 0.12
3132
import pandas.core.datetools as datetools
3233

3334
from pandas.core.config import get_option, set_option, reset_option,\

pandas/core/common.py

+37-48
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
"""
22
Misc tools for implementing data structures
33
"""
4-
# XXX: HACK for NumPy 1.5.1 to suppress warnings
5-
try:
6-
import cPickle as pickle
7-
except ImportError: # pragma: no cover
8-
import pickle
94

105
import itertools
116
from datetime import datetime
@@ -1668,49 +1663,6 @@ def _all_none(*args):
16681663
return True
16691664

16701665

1671-
def save(obj, path):
1672-
"""
1673-
Pickle (serialize) object to input file path
1674-
1675-
Parameters
1676-
----------
1677-
obj : any object
1678-
path : string
1679-
File path
1680-
"""
1681-
f = open(path, 'wb')
1682-
try:
1683-
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
1684-
finally:
1685-
f.close()
1686-
1687-
1688-
def load(path):
1689-
"""
1690-
Load pickled pandas object (or any other pickled object) from the specified
1691-
file path
1692-
1693-
Warning: Loading pickled data received from untrusted sources can be unsafe.
1694-
See: http://docs.python.org/2.7/library/pickle.html
1695-
1696-
Parameters
1697-
----------
1698-
path : string
1699-
File path
1700-
1701-
Returns
1702-
-------
1703-
unpickled : type of object stored in file
1704-
"""
1705-
try:
1706-
with open(path,'rb') as fh:
1707-
return pickle.load(fh)
1708-
except:
1709-
if not py3compat.PY3:
1710-
raise
1711-
with open(path,'rb') as fh:
1712-
return pickle.load(fh, encoding='latin1')
1713-
17141666
class UTF8Recoder:
17151667
"""
17161668
Iterator that reads an encoded stream and reencodes the input to UTF-8
@@ -2109,3 +2061,40 @@ def console_encode(object, **kwds):
21092061
"""
21102062
return pprint_thing_encoded(object,
21112063
get_option("display.encoding"))
2064+
2065+
def load(path): # TODO remove in 0.12
2066+
"""
2067+
Load pickled pandas object (or any other pickled object) from the specified
2068+
file path
2069+
2070+
Warning: Loading pickled data received from untrusted sources can be unsafe.
2071+
See: http://docs.python.org/2.7/library/pickle.html
2072+
2073+
Parameters
2074+
----------
2075+
path : string
2076+
File path
2077+
2078+
Returns
2079+
-------
2080+
unpickled : type of object stored in file
2081+
"""
2082+
import warnings
2083+
warnings.warn("load is deprecated, use read_pickle", FutureWarning)
2084+
from pandas.io.pickle import read_pickle
2085+
return read_pickle(path)
2086+
2087+
def save(obj, path): # TODO remove in 0.12
2088+
'''
2089+
Pickle (serialize) object to input file path
2090+
2091+
Parameters
2092+
----------
2093+
obj : any object
2094+
path : string
2095+
File path
2096+
'''
2097+
import warnings
2098+
warnings.warn("save is deprecated, use obj.to_pickle", FutureWarning)
2099+
from pandas.io.pickle import to_pickle
2100+
return to_pickle(obj, path)

pandas/core/generic.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,29 @@ class PandasObject(object):
2424
_AXIS_ALIASES = {}
2525
_AXIS_NAMES = dict((v, k) for k, v in _AXIS_NUMBERS.iteritems())
2626

27-
def save(self, path):
28-
com.save(self, path)
27+
def to_pickle(self, path):
28+
"""
29+
Pickle (serialize) object to input file path
2930
30-
@classmethod
31-
def load(cls, path):
32-
return com.load(path)
31+
Parameters
32+
----------
33+
path : string
34+
File path
35+
"""
36+
from pandas.io.pickle import to_pickle
37+
return to_pickle(self, path)
38+
39+
def save(self, path): # TODO remove in 0.12
40+
import warnings
41+
from pandas.io.pickle import to_pickle
42+
warnings.warn("save is deprecated, use to_pickle", FutureWarning)
43+
return to_pickle(self, path)
44+
45+
def load(self, path): # TODO remove in 0.12
46+
import warnings
47+
from pandas.io.pickle import read_pickle
48+
warnings.warn("load is deprecated, use pd.read_pickle", FutureWarning)
49+
return read_pickle(path)
3350

3451
def __hash__(self):
3552
raise TypeError('{0!r} objects are mutable, thus they cannot be'

pandas/io/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
from pandas.io.html import read_html
1111
from pandas.io.sql import read_sql
1212
from pandas.io.stata import read_stata
13+
from pandas.io.pickle import read_pickle

pandas/io/pickle.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# XXX: HACK for NumPy 1.5.1 to suppress warnings
2+
try:
3+
import cPickle as pickle
4+
except ImportError: # pragma: no cover
5+
import pickle
6+
7+
def to_pickle(obj, path):
8+
"""
9+
Pickle (serialize) object to input file path
10+
11+
Parameters
12+
----------
13+
obj : any object
14+
path : string
15+
File path
16+
"""
17+
f = open(path, 'wb')
18+
try:
19+
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
20+
finally:
21+
f.close()
22+
23+
def read_pickle(path):
24+
"""
25+
Load pickled pandas object (or any other pickled object) from the specified
26+
file path
27+
28+
Warning: Loading pickled data received from untrusted sources can be unsafe.
29+
See: http://docs.python.org/2.7/library/pickle.html
30+
31+
Parameters
32+
----------
33+
path : string
34+
File path
35+
36+
Returns
37+
-------
38+
unpickled : type of object stored in file
39+
"""
40+
try:
41+
with open(path,'rb') as fh:
42+
return pickle.load(fh)
43+
except:
44+
from pandas.util import py3compat
45+
if not py3compat.PY3:
46+
raise
47+
with open(path,'rb') as fh:
48+
return pickle.load(fh, encoding='latin1')

pandas/sparse/tests/test_sparse.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from numpy import nan
1111
import numpy as np
12+
import pandas as pd
1213
dec = np.testing.dec
1314

1415
from pandas.util.testing import (assert_almost_equal, assert_series_equal,

pandas/tests/test_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ def test_legacy_v2_unpickle(self):
10801080
pth, _ = os.path.split(os.path.abspath(__file__))
10811081
filepath = os.path.join(pth, 'data', 'mindex_073.pickle')
10821082

1083-
obj = com.load(filepath)
1083+
obj = pd.read_pickle(filepath)
10841084

10851085
obj2 = MultiIndex.from_tuples(obj.values)
10861086
self.assert_(obj.equals(obj2))

0 commit comments

Comments
 (0)