Skip to content

Commit

Permalink
Close FastParquet file even on error (#46556)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Mar 30, 2022
1 parent efb262f commit 8808db8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.5.0.rst
Expand Up @@ -561,7 +561,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
^^^^^^
Expand Down
13 changes: 6 additions & 7 deletions pandas/io/parquet.py
Expand Up @@ -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"])
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/test_parquet.py
Expand Up @@ -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)

0 comments on commit 8808db8

Please sign in to comment.