Skip to content

Commit f90f4aa

Browse files
authored
CLN: PY3 Remove cPickle (#25956)
* CLN: PY3 cPickle * add back pickle_compat import * Move pickle imports to top * Clarify pickle read operation
1 parent 485cbbb commit f90f4aa

File tree

6 files changed

+25
-62
lines changed

6 files changed

+25
-62
lines changed

pandas/compat/__init__.py

-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@
4444
# always writeable
4545
from StringIO import StringIO
4646
BytesIO = StringIO
47-
import cPickle
4847
import httplib
4948
except ImportError:
5049
import builtins
5150
from io import StringIO, BytesIO
5251
cStringIO = StringIO
53-
import pickle as cPickle
5452
import http.client as httplib
5553

5654
from pandas.compat.chainmap import DeepChainMap

pandas/compat/pickle_compat.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88

99
import pandas # noqa
10-
from pandas import Index, compat
10+
from pandas import Index
1111

1212

1313
def load_reduce(self):
@@ -138,27 +138,14 @@ def load_reduce(self):
138138
# our Unpickler sub-class to override methods and some dispatcher
139139
# functions for compat
140140

141-
if compat.PY3:
142-
class Unpickler(pkl._Unpickler):
141+
class Unpickler(pkl._Unpickler):
143142

144-
def find_class(self, module, name):
145-
# override superclass
146-
key = (module, name)
147-
module, name = _class_locations_map.get(key, key)
148-
return super(Unpickler, self).find_class(module, name)
143+
def find_class(self, module, name):
144+
# override superclass
145+
key = (module, name)
146+
module, name = _class_locations_map.get(key, key)
147+
return super(Unpickler, self).find_class(module, name)
149148

150-
else:
151-
152-
class Unpickler(pkl.Unpickler):
153-
154-
def find_class(self, module, name):
155-
# override superclass
156-
key = (module, name)
157-
module, name = _class_locations_map.get(key, key)
158-
__import__(module)
159-
mod = sys.modules[module]
160-
klass = getattr(mod, name)
161-
return klass
162149

163150
Unpickler.dispatch = copy.copy(Unpickler.dispatch)
164151
Unpickler.dispatch[pkl.REDUCE[0]] = load_reduce

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import gc
66
import json
77
import operator
8+
import pickle
89
from textwrap import dedent
910
import warnings
1011
import weakref
@@ -15,8 +16,7 @@
1516

1617
from pandas._libs import Timestamp, iNaT, properties
1718
import pandas.compat as compat
18-
from pandas.compat import (
19-
cPickle as pkl, isidentifier, lrange, lzip, set_function_name, to_str)
19+
from pandas.compat import isidentifier, lrange, lzip, set_function_name, to_str
2020
from pandas.compat.numpy import function as nv
2121
from pandas.errors import AbstractMethodError
2222
from pandas.util._decorators import (
@@ -2564,7 +2564,7 @@ def to_sql(self, name, con, schema=None, if_exists='fail', index=True,
25642564
dtype=dtype, method=method)
25652565

25662566
def to_pickle(self, path, compression='infer',
2567-
protocol=pkl.HIGHEST_PROTOCOL):
2567+
protocol=pickle.HIGHEST_PROTOCOL):
25682568
"""
25692569
Pickle (serialize) object to file.
25702570

pandas/io/pickle.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
""" pickle compat """
2+
import pickle
23
import warnings
34

45
from numpy.lib.format import read_array
56

6-
from pandas.compat import BytesIO, cPickle as pkl, pickle_compat as pc
7+
from pandas.compat import BytesIO, pickle_compat as pc
78

89
from pandas.io.common import _get_handle, _stringify_path
910

1011

11-
def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
12+
def to_pickle(obj, path, compression='infer',
13+
protocol=pickle.HIGHEST_PROTOCOL):
1214
"""
1315
Pickle (serialize) object to file.
1416
@@ -71,9 +73,9 @@ def to_pickle(obj, path, compression='infer', protocol=pkl.HIGHEST_PROTOCOL):
7173
compression=compression,
7274
is_text=False)
7375
if protocol < 0:
74-
protocol = pkl.HIGHEST_PROTOCOL
76+
protocol = pickle.HIGHEST_PROTOCOL
7577
try:
76-
f.write(pkl.dumps(obj, protocol=protocol))
78+
f.write(pickle.dumps(obj, protocol=protocol))
7779
finally:
7880
f.close()
7981
for _f in fh:
@@ -140,15 +142,15 @@ def read_pickle(path, compression='infer'):
140142
path = _stringify_path(path)
141143
f, fh = _get_handle(path, 'rb', compression=compression, is_text=False)
142144

143-
# 1) try with cPickle
144-
# 2) try with the compat pickle to handle subclass changes
145-
# 3) pass encoding only if its not None as py2 doesn't handle the param
145+
# 1) try standard libary Pickle
146+
# 2) try pickle_compat (older pandas version) to handle subclass changes
147+
# 3) try pickle_compat with latin1 encoding
146148

147149
try:
148150
with warnings.catch_warnings(record=True):
149151
# We want to silence any warnings about, e.g. moved modules.
150152
warnings.simplefilter("ignore", Warning)
151-
return pkl.load(f)
153+
return pickle.load(f)
152154
except Exception: # noqa: E722
153155
try:
154156
return pc.load(f, encoding=None)

pandas/tests/io/generate_legacy_storage_files.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from datetime import timedelta
3838
from distutils.version import LooseVersion
3939
import os
40+
import pickle
4041
import platform as pl
4142
import sys
4243

@@ -285,11 +286,6 @@ def platform_name():
285286
def write_legacy_pickles(output_dir):
286287

287288
# make sure we are < 0.13 compat (in py3)
288-
try:
289-
from pandas.compat import cPickle as pickle # noqa
290-
except ImportError:
291-
import pickle
292-
293289
version = pandas.__version__
294290

295291
print("This script generates a storage file for the current arch, system, "

pandas/tests/io/test_pickle.py

+4-24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import gzip
1919
import lzma
2020
import os
21+
import pickle
2122
import shutil
2223
from warnings import catch_warnings, simplefilter
2324
import zipfile
@@ -217,37 +218,20 @@ def test_pickles(current_pickle_data, legacy_pickle):
217218

218219
def test_round_trip_current(current_pickle_data):
219220

220-
try:
221-
import cPickle as c_pickle
222-
223-
def c_pickler(obj, path):
224-
with open(path, 'wb') as fh:
225-
c_pickle.dump(obj, fh, protocol=-1)
226-
227-
def c_unpickler(path):
228-
with open(path, 'rb') as fh:
229-
fh.seek(0)
230-
return c_pickle.load(fh)
231-
except ImportError:
232-
c_pickler = None
233-
c_unpickler = None
234-
235-
import pickle as python_pickle
236-
237221
def python_pickler(obj, path):
238222
with open(path, 'wb') as fh:
239-
python_pickle.dump(obj, fh, protocol=-1)
223+
pickle.dump(obj, fh, protocol=-1)
240224

241225
def python_unpickler(path):
242226
with open(path, 'rb') as fh:
243227
fh.seek(0)
244-
return python_pickle.load(fh)
228+
return pickle.load(fh)
245229

246230
data = current_pickle_data
247231
for typ, dv in data.items():
248232
for dt, expected in dv.items():
249233

250-
for writer in [pd.to_pickle, c_pickler, python_pickler]:
234+
for writer in [pd.to_pickle, python_pickler]:
251235
if writer is None:
252236
continue
253237

@@ -260,10 +244,6 @@ def python_unpickler(path):
260244
result = pd.read_pickle(path)
261245
compare_element(result, expected, typ)
262246

263-
if c_unpickler is not None:
264-
result = c_unpickler(path)
265-
compare_element(result, expected, typ)
266-
267247
result = python_unpickler(path)
268248
compare_element(result, expected, typ)
269249

0 commit comments

Comments
 (0)