-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
BUG: Fix empty MultiIndex DataFrame xlsx export #57697
Conversation
MultiIndex on both axes, with zero rows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
) | ||
df.to_excel(tmp_excel) | ||
result = pd.read_excel(tmp_excel, header=None) | ||
assert result.iloc[:2, -1].to_list() == ["A", "B"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you test the entire result? I think if you change the read line to
result = pd.read_excel("tmp.xlsx", header=[0, 1], index_col=[0, 1])
Then you should just be able to do
tm.assert_frame_equal(result, df)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, that looks better and works exactly like you said.
doc/source/whatsnew/v3.0.0.rst
Outdated
@@ -324,6 +324,7 @@ MultiIndex | |||
|
|||
I/O | |||
^^^ | |||
- Bug in :class:`io.formats.excel.ExcelFormatter` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe ExcelFormatter
is private; can you change to to :meth:`DataFrame.to_excel`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Thanks @speleo3 - nice fix! |
Thanks a lot for fixing this. I'm running into this exact issue at work. Since 3.0.0 hasn't been released yet, is there a workaround in the meantime? |
My attempt: if df.empty and not df.columns:
df.columns = [] |
Fix Excel xlsx export of empty (zero rows) DataFrame with MultiIndex on both axes.
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.