Skip to content

io/msgpack: Raise a more informative exception on file not found #27162

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -817,6 +817,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`)
- 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`)

Plotting
^^^^^^^^
Expand Down
13 changes: 5 additions & 8 deletions pandas/io/packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,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)
# Note, we immediately try to read the file here if we
# got a string, as opposed to trying to parse it as something
# else. See #27160.
with open(path_or_buf, 'rb') as fh:
return read(fh)

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 @@ -140,7 +140,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'),
])
def test_read_non_existant(self, reader, module, error_class, fn_ext):
Expand Down Expand Up @@ -169,7 +169,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'),
])
def test_read_expands_user_home_dir(self, reader, module,
Expand Down