-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Excel import crashes on simple file #48706
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
Comments
Here is the Excel file in question for reproducing the problem |
On a second thought, I'm not sure what the expected behavior should be and why it didn't crash in 1.4.3? |
Previously, these rows were just ignored, which returned weird results. we are strict when validating the header rows since 1.5. Why specifying non-existing header rows at all? So the new behaviour is expected. |
Alright, thanks for the info! |
@phofl Sorry for re-opening, but I'm a bit torn apart by this new behavior after trying to fix everything on our side, it seems it will be impossible for us to use Pandas 1.5.X because of this problem: We regularly have Excel documents with multiple sheets. Some of these sheets might be empty or not have a header - that's OK. There is only one My problem with the current solution is:
|
Sure, an error message indicating which sheet this was in might be good
Not overly keen on this kind of behaviour - why not do something like sheet_names = pd.ExcelFile("/Users/alex/Desktop/BrokenExcel.xlsx").sheet_names
output = {}
for sheet_name in sheet_names:
try:
df = pd.read_excel("/Users/alex/Desktop/BrokenExcel.xlsx", sheet_name=sheet_name, header=1)
except ValueError:
continue
output[sheet_name] = df ? |
@MarcoGorelli Yes, having a more descriptive error message would be fantastic. Thanks also for the proposed solution - I will change our code to read the sheets like this! 🙏 |
Can I work on this issue? I can start by exploring the changes required to include the name of the offending sheet in the error message EDIT: I tried to avoid modifying a large portion of code while also respecting the responsibility of each of the parser classes. Also, this seems to generalize to other diff --git a/pandas/io/excel/_base.py b/pandas/io/excel/_base.py
index f555e7c5f5..3b12868144 100644
--- a/pandas/io/excel/_base.py
+++ b/pandas/io/excel/_base.py
@@ -897,6 +897,10 @@ class BaseExcelReader(metaclass=abc.ABCMeta):
asheetname
].columns.set_names(header_names)
+ except ValueError as err:
+ err.args = (f"{err.args[0]} (Sheet: {asheetname})", *err.args[1:])
+ raise err.with_traceback(err.__traceback__)
+
except EmptyDataError:
# No Data, return an empty DataFrame
output[asheetname] = DataFrame() This results in the following exception message:
Full traceback
|
nice - nitpick would to have |
Sure, I'll apply the change and make the PR soon |
@MarcoGorelli , the linked PR #49186 is ready for review 👌 BTW, should this issue be renamed? It ended up not being a bug but a feature request it seems |
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
Loading the attached file produces an exception and crashes in 1.5.0 but worked in 1.4.3.
Expected Behavior
Loading of the Excel file (as in 1.4.3)
Installed Versions
INSTALLED VERSIONS
commit : 87cfe4e
python : 3.9.13.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.5.0
numpy : 1.23.3
pytz : 2022.1
dateutil : 2.8.2
setuptools : 62.6.0
pip : 22.2.2
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: