Skip to content

Commit 679dbd0

Browse files
Kanejreback
Kane
authored andcommitted
Misleading error for pd.read_msgpack (#27201)
1 parent 8794516 commit 679dbd0

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ I/O
10941094
- Bug while selecting from :class:`HDFStore` with ``where=''`` specified (:issue:`26610`).
10951095
- 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`)
10961096
- Bug in :meth:`read_hdf` where reading a timezone aware :class:`DatetimeIndex` would raise a ``TypeError`` (:issue:`11926`)
1097+
- Bug in :meth:`to_msgpack` and :meth:`read_msgpack` which would raise a ``ValueError`` rather than a ``FileNotFoundError`` for an invalid path (:issue:`27160`)
10971098
10981099
Plotting
10991100
^^^^^^^^

pandas/core/generic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,17 +2560,17 @@ def to_msgpack(self, path_or_buf=None, encoding="utf-8", **kwargs):
25602560
Parameters
25612561
----------
25622562
path : string File path, buffer-like, or None
2563-
if None, return generated string
2563+
if None, return generated bytes
25642564
append : bool whether to append to an existing msgpack
25652565
(default is False)
25662566
compress : type of compressor (zlib or blosc), default to None (no
25672567
compression)
25682568
25692569
Returns
25702570
-------
2571-
None or str
2571+
None or bytes
25722572
If path_or_buf is None, returns the resulting msgpack format as a
2573-
string. Otherwise returns None.
2573+
byte string. Otherwise returns None.
25742574
"""
25752575

25762576
from pandas.io import packers

pandas/io/packers.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def to_msgpack(path_or_buf, *args, **kwargs):
108108
Parameters
109109
----------
110110
path_or_buf : string File path, buffer-like, or None
111-
if None, return generated string
111+
if None, return generated bytes
112112
args : an object or objects to serialize
113113
encoding : encoding for unicode objects
114114
append : boolean whether to append to an existing msgpack
@@ -139,8 +139,12 @@ def writer(fh):
139139

140140
path_or_buf = _stringify_path(path_or_buf)
141141
if isinstance(path_or_buf, str):
142-
with open(path_or_buf, mode) as fh:
143-
writer(fh)
142+
try:
143+
with open(path_or_buf, mode) as fh:
144+
writer(fh)
145+
except FileNotFoundError:
146+
msg = "File b'{}' does not exist".format(path_or_buf)
147+
raise FileNotFoundError(msg)
144148
elif path_or_buf is None:
145149
buf = BytesIO()
146150
writer(buf)
@@ -204,13 +208,11 @@ def read(fh):
204208
# see if we have an actual file
205209
if isinstance(path_or_buf, str):
206210
try:
207-
exists = os.path.exists(path_or_buf)
208-
except (TypeError, ValueError):
209-
exists = False
210-
211-
if exists:
212211
with open(path_or_buf, "rb") as fh:
213212
return read(fh)
213+
except FileNotFoundError:
214+
msg = "File b'{}' does not exist".format(path_or_buf)
215+
raise FileNotFoundError(msg)
214216

215217
if isinstance(path_or_buf, bytes):
216218
# treat as a binary-like

pandas/tests/io/test_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_iterator(self):
142142
(pd.read_stata, "os", FileNotFoundError, "dta"),
143143
(pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
144144
(pd.read_json, "os", ValueError, "json"),
145-
(pd.read_msgpack, "os", ValueError, "mp"),
145+
(pd.read_msgpack, "os", FileNotFoundError, "mp"),
146146
(pd.read_pickle, "os", FileNotFoundError, "pickle"),
147147
],
148148
)
@@ -177,7 +177,7 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
177177
(pd.read_stata, "os", FileNotFoundError, "dta"),
178178
(pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
179179
(pd.read_json, "os", ValueError, "json"),
180-
(pd.read_msgpack, "os", ValueError, "mp"),
180+
(pd.read_msgpack, "os", FileNotFoundError, "mp"),
181181
(pd.read_pickle, "os", FileNotFoundError, "pickle"),
182182
],
183183
)

pandas/tests/io/test_packers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,15 @@ def __init__(self):
165165
self.read = 0
166166

167167
msg = "Invalid file path or buffer object type: <class '{}'>"
168+
invalid_path = os.path.join("nonexistent_dir", "df.msgpack")
168169
with pytest.raises(ValueError, match=msg.format("NoneType")):
169170
read_msgpack(path_or_buf=None)
170171
with pytest.raises(ValueError, match=msg.format("dict")):
171172
read_msgpack(path_or_buf={})
172173
with pytest.raises(ValueError, match=msg.format(r".*\.A")):
173174
read_msgpack(path_or_buf=A())
175+
with pytest.raises(FileNotFoundError, match="does not exist"):
176+
read_msgpack(path_or_buf=invalid_path)
174177

175178

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

0 commit comments

Comments
 (0)