diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 6cbee83247692..48a52f21e3640 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -498,7 +498,8 @@ I/O - Bug in :func:`read_parquet` when ``engine="pyarrow"`` which caused partial write to disk when column of unsupported datatype was passed (:issue:`44914`) - Bug in :func:`DataFrame.to_excel` and :class:`ExcelWriter` would raise when writing an empty DataFrame to a ``.ods`` file (:issue:`45793`) - Bug in Parquet roundtrip for Interval dtype with ``datetime64[ns]`` subtype (:issue:`45881`) -- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements(:issue:`45598`) +- Bug in :func:`read_excel` when reading a ``.ods`` file with newlines between xml elements (:issue:`45598`) +- Bug in :func:`read_parquet` when ``engine="fastparquet"`` where the file was not closed on error (:issue:`46555`) Period ^^^^^^ diff --git a/pandas/io/parquet.py b/pandas/io/parquet.py index c7e8d67189e5d..27b0b3d08ad53 100644 --- a/pandas/io/parquet.py +++ b/pandas/io/parquet.py @@ -349,13 +349,12 @@ def read( ) path = handles.handle - parquet_file = self.api.ParquetFile(path, **parquet_kwargs) - - result = parquet_file.to_pandas(columns=columns, **kwargs) - - if handles is not None: - handles.close() - return result + try: + parquet_file = self.api.ParquetFile(path, **parquet_kwargs) + return parquet_file.to_pandas(columns=columns, **kwargs) + finally: + if handles is not None: + handles.close() @doc(storage_options=_shared_docs["storage_options"]) diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 3001922e95a54..7c04a51e803f6 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -1155,3 +1155,11 @@ def test_use_nullable_dtypes_not_supported(self, fp): df.to_parquet(path) with pytest.raises(ValueError, match="not supported for the fastparquet"): read_parquet(path, engine="fastparquet", use_nullable_dtypes=True) + + def test_close_file_handle_on_read_error(self): + with tm.ensure_clean("test.parquet") as path: + pathlib.Path(path).write_bytes(b"breakit") + with pytest.raises(Exception, match=""): # Not important which exception + read_parquet(path, engine="fastparquet") + # The next line raises an error on Windows if the file is still open + pathlib.Path(path).unlink(missing_ok=False)