Skip to content

CLN: Exception in pickle loading #28645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions doc/source/whatsnew/v0.8.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,15 @@ Other new features
New plotting methods
~~~~~~~~~~~~~~~~~~~~

.. ipython:: python
:suppress:
.. code-block:: python

import pandas as pd
fx = pd.read_pickle('data/fx_prices')
import matplotlib.pyplot as plt

``Series.plot`` now supports a ``secondary_y`` option:

.. ipython:: python
.. code-block:: python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you turn those again into ipython blocks now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbrockmendel do we need to revert this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was reverted and then un-reverted, following #28645 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool


plt.figure()

Expand Down
23 changes: 3 additions & 20 deletions pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import copy
import pickle as pkl
import sys
from typing import TYPE_CHECKING
import warnings

Expand All @@ -25,38 +24,22 @@ def load_reduce(self):
try:
stack[-1] = func(*args)
return
except Exception as e:
except TypeError as err:

# If we have a deprecated function,
# try to replace and try again.

msg = "_reconstruct: First argument must be a sub-type of ndarray"

if msg in str(e):
if msg in str(err):
try:
cls = args[0]
stack[-1] = object.__new__(cls)
return
except TypeError:
pass

# try to re-encode the arguments
if getattr(self, "encoding", None) is not None:
args = tuple(
arg.encode(self.encoding) if isinstance(arg, str) else arg
for arg in args
)
try:
stack[-1] = func(*args)
return
except TypeError:
pass

# unknown exception, re-raise
if getattr(self, "is_verbose", None):
print(sys.exc_info())
print(func, args)
raise
raise err


_sparse_msg = """\
Expand Down
20 changes: 13 additions & 7 deletions pandas/io/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from numpy.lib.format import read_array

from pandas.compat import pickle_compat as pc
from pandas.compat import PY36, pickle_compat as pc

from pandas.io.common import _get_handle, _stringify_path

Expand Down Expand Up @@ -142,18 +142,24 @@ def read_pickle(path, compression="infer"):

# 1) try standard libary Pickle
# 2) try pickle_compat (older pandas version) to handle subclass changes
# 3) try pickle_compat with latin1 encoding

excs_to_catch = (AttributeError, ImportError)
if PY36:
excs_to_catch += (ModuleNotFoundError,)

try:
with warnings.catch_warnings(record=True):
# We want to silence any warnings about, e.g. moved modules.
warnings.simplefilter("ignore", Warning)
return pickle.load(f)
except Exception:
try:
return pc.load(f, encoding=None)
except Exception:
return pc.load(f, encoding="latin1")
except excs_to_catch:
# e.g.
# "No module named 'pandas.core.sparse.series'"
# "Can't get attribute '__nat_unpickle' on <module 'pandas._libs.tslib"
return pc.load(f, encoding=None)
except UnicodeDecodeError:
# e.g. can occur for files written in py27; see GH#28645
return pc.load(f, encoding="latin-1")
finally:
f.close()
for _f in fh:
Expand Down
Binary file added pandas/tests/io/data/test_py27.pkl
Binary file not shown.
11 changes: 11 additions & 0 deletions pandas/tests/io/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,14 @@ def test_read(self, protocol, get_random_path):
df.to_pickle(path, protocol=protocol)
df2 = pd.read_pickle(path)
tm.assert_frame_equal(df, df2)


def test_unicode_decode_error():
# pickle file written with py27, should be readable without raising
# UnicodeDecodeError, see GH#28645
path = os.path.join(os.path.dirname(__file__), "data", "test_py27.pkl")
df = pd.read_pickle(path)

# just test the columns are correct since the values are random
excols = pd.Index(["a", "b", "c"])
tm.assert_index_equal(df.columns, excols)