Skip to content

ENH: Add sheet name to error message when reading Excel #49186

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

Merged
merged 9 commits into from
Oct 24, 2022
Merged
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ MultiIndex
I/O
^^^
- Bug in :func:`read_sas` caused fragmentation of :class:`DataFrame` and raised :class:`.errors.PerformanceWarning` (:issue:`48595`)
- Improved error message in :func:`read_excel` by including the offending sheet name when an exception is raised while reading a file (:issue:`48706`)
- Bug when a pickling a subset PyArrow-backed data that would serialize the entire data instead of the subset (:issue:`42600`)
- Bug in :func:`read_csv` for a single-line csv with fewer columns than ``names`` raised :class:`.errors.ParserError` with ``engine="c"`` (:issue:`47566`)
-
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,10 @@ def parse(
# No Data, return an empty DataFrame
output[asheetname] = DataFrame()

except Exception as err:
err.args = (f"{err.args[0]} (sheet: {asheetname})", *err.args[1:])
raise err

if ret_dict:
return output
else:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/io/excel/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,13 @@ def test_read_excel_blank_with_header(self, read_ext):
actual = pd.read_excel("blank_with_header" + read_ext, sheet_name="Sheet1")
tm.assert_frame_equal(actual, expected)

def test_exception_message_includes_sheet_name(self, read_ext):
# GH 48706
with pytest.raises(ValueError, match=r" \(sheet: Sheet1\)$"):
pd.read_excel("blank_with_header" + read_ext, header=[1], sheet_name=None)
with pytest.raises(ZeroDivisionError, match=r" \(sheet: Sheet1\)$"):
pd.read_excel("test1" + read_ext, usecols=lambda x: 1 / 0, sheet_name=None)

@pytest.mark.filterwarnings("ignore:Cell A4 is marked:UserWarning:openpyxl")
def test_date_conversion_overflow(self, request, engine, read_ext):
# GH 10001 : pandas.ExcelFile ignore parse_dates=False
Expand Down