Skip to content

Commit b71f33d

Browse files
author
Sam Spilsbury
committed
io/packers: Just let python raise itself if the file was not found
As opposed to checking if it exists first.
1 parent ad2e98c commit b71f33d

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ I/O
817817
- Bug while selecting from :class:`HDFStore` with ``where=''`` specified (:issue:`26610`).
818818
- 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`)
819819
- Bug in :meth:`read_hdf` where reading a timezone aware :class:`DatetimeIndex` would raise a ``TypeError`` (:issue:`11926`)
820+
- Raise ``FileNotFoundError`` instead of ``ValueError`` in :func:`read_msgpack` where a string or path-like object is passed and the referred-to file cannot be found on the filesystem (:issue:`21762`)
820821

821822
Plotting
822823
^^^^^^^^

pandas/io/packers.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,11 @@ def read(fh):
176176

177177
# see if we have an actual file
178178
if isinstance(path_or_buf, str):
179-
try:
180-
exists = os.path.exists(path_or_buf)
181-
except (TypeError, ValueError):
182-
exists = False
183-
184-
if exists:
185-
with open(path_or_buf, 'rb') as fh:
186-
return read(fh)
179+
# Note, we immediately try to read the file here if we
180+
# got a string, as opposed to trying to parse it as something
181+
# else. See #27160.
182+
with open(path_or_buf, 'rb') as fh:
183+
return read(fh)
187184

188185
if isinstance(path_or_buf, bytes):
189186
# 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
@@ -140,7 +140,7 @@ def test_iterator(self):
140140
(pd.read_stata, 'os', FileNotFoundError, 'dta'),
141141
(pd.read_sas, 'os', FileNotFoundError, 'sas7bdat'),
142142
(pd.read_json, 'os', ValueError, 'json'),
143-
(pd.read_msgpack, 'os', ValueError, 'mp'),
143+
(pd.read_msgpack, 'os', FileNotFoundError, 'mp'),
144144
(pd.read_pickle, 'os', FileNotFoundError, 'pickle'),
145145
])
146146
def test_read_non_existant(self, reader, module, error_class, fn_ext):
@@ -169,7 +169,7 @@ def test_read_non_existant(self, reader, module, error_class, fn_ext):
169169
(pd.read_stata, 'os', FileNotFoundError, 'dta'),
170170
(pd.read_sas, 'os', FileNotFoundError, 'sas7bdat'),
171171
(pd.read_json, 'os', ValueError, 'json'),
172-
(pd.read_msgpack, 'os', ValueError, 'mp'),
172+
(pd.read_msgpack, 'os', FileNotFoundError, 'mp'),
173173
(pd.read_pickle, 'os', FileNotFoundError, 'pickle'),
174174
])
175175
def test_read_expands_user_home_dir(self, reader, module,

0 commit comments

Comments
 (0)