diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 1390e60179aae..a504e269ff12d 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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 ^^^^^^^^ diff --git a/pandas/io/packers.py b/pandas/io/packers.py index 30e51e62aa764..508caa99b6b52 100644 --- a/pandas/io/packers.py +++ b/pandas/io/packers.py @@ -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 diff --git a/pandas/tests/io/test_common.py b/pandas/tests/io/test_common.py index 04faf5aee4b6d..a69606decf228 100644 --- a/pandas/tests/io/test_common.py +++ b/pandas/tests/io/test_common.py @@ -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): @@ -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,