-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: to_excel to throw IndexError for dataframe with empty (row) MultiIndex #19543
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
This is normal and expected behaviour. When taking a subset of a MultiIndex, the
This could be considered a bug, given that for an empty dataframe without multiindex, this works. Or at least it should give a better error message. The fact that |
Alright, so this issue should be focused on |
OK, I edited the title to reflect this. Probably the most sensible thing to do is to be consistent with empty dataframes in general (writing only the column headers to excel) PR welcome! |
Hello, |
I encountered this error on 1.2.4 having produced such an empty-multiindexed DataFrame via Simple sample of such, both as an example of this problem naturally arising from
|
If I see this correctly, it would suffice to reference pandas/pandas/io/formats/excel.py Line 637 in 46bcf37
I will try to construct a fix. |
Output:
The next line causes an
IndexError
:Notice that the 'empty'
MultiIndex
does not give any indication of its presence when the entire dataframe is printed, looking like an ordinary empty dataframe instead.Problem description
I'm not quite sure which behaviour is the problematic one here: the fact that the first block of code creates an 'empty'
MultiIndex
that has values inlevels
, or thatto_excel
doesn't write aDataFrame
with such an index to file properly. The 'empty'MultiIndex
behaviour is a little bit anomalous by itself, because a similar block of code (below) will cause aKeyError
. TheKeyError
is 'suppressed' by the fact that there is a third level in theMultiIndex
.I initially ran into this behaviour (the IndexError) when using
xs
, but sincexs
seems to be headed for the deprecation guillotine, I figured I'd rewrite this withloc
instead.Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-43-Microsoft
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: None
openpyxl: 2.4.9
xlrd: 1.1.0
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0b10
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: