Skip to content

Misleading error for pd.read_msgpack #27201

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 16 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,7 @@ I/O
- Bug while selecting from :class:`HDFStore` with ``where=''`` specified (:issue:`26610`).
- Fixed bug in :func:`DataFrame.to_excel()` where custom objects (i.e. `PeriodIndex`) inside merged cells were not being converted into types safe for the Excel writer (:issue:`27006`)
- Bug in :meth:`read_hdf` where reading a timezone aware :class:`DatetimeIndex` would raise a ``TypeError`` (:issue:`11926`)
- Bug in :meth:`to_msgpack` and :meth:`read_msgpack` which would raise a ``ValueError`` rather than a ``FileNotFoundError`` for an invalid path (:issue:`27160`)

Plotting
^^^^^^^^
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2560,17 +2560,17 @@ def to_msgpack(self, path_or_buf=None, encoding="utf-8", **kwargs):
Parameters
----------
path : string File path, buffer-like, or None
if None, return generated string
if None, return generated bytes
append : bool whether to append to an existing msgpack
(default is False)
compress : type of compressor (zlib or blosc), default to None (no
compression)

Returns
-------
None or str
None or bytes
If path_or_buf is None, returns the resulting msgpack format as a
string. Otherwise returns None.
byte string. Otherwise returns None.
"""

from pandas.io import packers
Expand Down
18 changes: 10 additions & 8 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def to_msgpack(path_or_buf, *args, **kwargs):
Parameters
----------
path_or_buf : string File path, buffer-like, or None
if None, return generated string
if None, return generated bytes
args : an object or objects to serialize
encoding : encoding for unicode objects
append : boolean whether to append to an existing msgpack
Expand Down Expand Up @@ -139,8 +139,12 @@ def writer(fh):

path_or_buf = _stringify_path(path_or_buf)
if isinstance(path_or_buf, str):
with open(path_or_buf, mode) as fh:
writer(fh)
try:
with open(path_or_buf, mode) as fh:
writer(fh)
except FileNotFoundError:
msg = "File b'{}' does not exist".format(path_or_buf)
raise FileNotFoundError(msg)
elif path_or_buf is None:
buf = BytesIO()
writer(buf)
Expand Down Expand Up @@ -204,13 +208,11 @@ def read(fh):
# see if we have an actual file
if isinstance(path_or_buf, str):
try:
exists = os.path.exists(path_or_buf)
except (TypeError, ValueError):
exists = False

if exists:
with open(path_or_buf, "rb") as fh:
return read(fh)
except FileNotFoundError:
msg = "File b'{}' does not exist".format(path_or_buf)
raise FileNotFoundError(msg)

if isinstance(path_or_buf, bytes):
# treat as a binary-like
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_iterator(self):
(pd.read_stata, "os", FileNotFoundError, "dta"),
(pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
(pd.read_json, "os", ValueError, "json"),
(pd.read_msgpack, "os", ValueError, "mp"),
(pd.read_msgpack, "os", FileNotFoundError, "mp"),
(pd.read_pickle, "os", FileNotFoundError, "pickle"),
],
)
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
(pd.read_stata, "os", FileNotFoundError, "dta"),
(pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
(pd.read_json, "os", ValueError, "json"),
(pd.read_msgpack, "os", ValueError, "mp"),
(pd.read_msgpack, "os", FileNotFoundError, "mp"),
(pd.read_pickle, "os", FileNotFoundError, "pickle"),
],
)
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/io/test_packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,15 @@ def __init__(self):
self.read = 0

msg = "Invalid file path or buffer object type: <class '{}'>"
invalid_path = os.path.join("nonexistent_dir", "df.msgpack")
with pytest.raises(ValueError, match=msg.format("NoneType")):
read_msgpack(path_or_buf=None)
with pytest.raises(ValueError, match=msg.format("dict")):
read_msgpack(path_or_buf={})
with pytest.raises(ValueError, match=msg.format(r".*\.A")):
read_msgpack(path_or_buf=A())
with pytest.raises(FileNotFoundError, match="does not exist"):
read_msgpack(path_or_buf=invalid_path)


@pytest.mark.filterwarnings("ignore:.*msgpack:FutureWarning")
Expand Down