Skip to content
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

Merged
merged 2 commits into from
Mar 2, 2024

Conversation

speleo3
Copy link
Contributor

@speleo3 speleo3 commented Mar 1, 2024

Fix Excel xlsx export of empty (zero rows) DataFrame with MultiIndex on both axes.

Copy link
Member

@rhshadrach rhshadrach left a 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"]
Copy link
Member

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)

Copy link
Contributor Author

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.

@@ -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`)
Copy link
Member

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`

@rhshadrach rhshadrach added Bug IO Excel read_excel, to_excel MultiIndex labels Mar 1, 2024
@rhshadrach rhshadrach added this to the 3.0 milestone Mar 1, 2024
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@rhshadrach rhshadrach merged commit ddc3144 into pandas-dev:main Mar 2, 2024
45 of 47 checks passed
@rhshadrach
Copy link
Member

Thanks @speleo3 - nice fix!

@speleo3 speleo3 deleted the fix-multiindex-xlsx-57696 branch March 2, 2024 12:37
pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
@xjcl
Copy link

xjcl commented Sep 16, 2024

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?

@xjcl
Copy link

xjcl commented Sep 16, 2024

My attempt:

if df.empty and not df.columns:
    df.columns = []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel MultiIndex
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Error when exporting empty DataFrame with MultiIndex on both axes to xlsx file
3 participants