Skip to content

Commit fbb6ddc

Browse files
committed
Add new parameter to check if file is empty raise error or return empty DataFrame
1 parent 543680d commit fbb6ddc

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Diff for: pandas/io/parsers/readers.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import numpy as np
2828

29+
import pandas.errors
2930
from pandas._libs import lib
3031
from pandas._libs.parsers import STR_NA_VALUES
3132
from pandas.errors import (
@@ -709,8 +710,17 @@ def _read(
709710
# Check for duplicates in names.
710711
_validate_names(kwds.get("names", None))
711712

712-
# Create the parser.
713-
parser = TextFileReader(filepath_or_buffer, **kwds)
713+
# Check for empty file.
714+
try:
715+
# Create the parser.
716+
parser = TextFileReader(filepath_or_buffer, **kwds)
717+
except pandas.errors.EmptyDataError:
718+
if kwds.get("return_empty", False):
719+
raise
720+
else:
721+
print("Empty DataFrame")
722+
return DataFrame()
723+
714724

715725
if chunksize or iterator:
716726
return parser
@@ -832,6 +842,7 @@ def read_csv(
832842
float_precision: Literal["high", "legacy", "round_trip"] | None = None,
833843
storage_options: StorageOptions | None = None,
834844
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
845+
return_empty: bool = False,
835846
) -> DataFrame | TextFileReader:
836847
# locals() should never be modified
837848
kwds = locals().copy()
@@ -968,6 +979,7 @@ def read_table(
968979
float_precision: Literal["high", "legacy", "round_trip"] | None = None,
969980
storage_options: StorageOptions | None = None,
970981
dtype_backend: DtypeBackend | lib.NoDefault = lib.no_default,
982+
return_empty: bool = False,
971983
) -> DataFrame | TextFileReader:
972984
# locals() should never be modified
973985
kwds = locals().copy()

0 commit comments

Comments
 (0)