-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG : Fix Excel header NaN duplication with merged MultiIndex columns in to_excel #62576
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
5f70c16
1b5fe97
77422bf
4d2b7af
04cd920
9b84372
0a277de
b7f867a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1583,6 +1583,29 @@ def assert_called_and_reset(cls): | |
| df.to_excel(filepath2, engine="dummy") | ||
| DummyClass.assert_called_and_reset() | ||
|
|
||
| @td.skip_if_no("openpyxl") | ||
| def test_to_excel_multiindex_nan_in_columns(self, merge_cells): | ||
| # GH 62340 | ||
| df = ( | ||
| DataFrame({"a": list("ABBAAAB"), "b": [-1, 1, 1, -2, float("nan"), 3, -4]}) | ||
| .assign(b_bin=lambda x: pd.cut(x.b, bins=[-float("inf"), 0, float("inf")])) | ||
| .groupby(["b_bin", "a"], as_index=False, observed=True, dropna=False) | ||
| .agg(b_sum=("b", "sum"), b_prod=("b", "prod")) | ||
| .pivot(index="a", columns="b_bin", values=["b_sum", "b_prod"]) | ||
| ) | ||
|
|
||
| with ExcelWriter("test.xlsx", engine="openpyxl") as writer: | ||
| df.to_excel(writer, sheet_name="Sheet1", merge_cells=merge_cells) | ||
|
|
||
| reader = ExcelFile("test.xlsx") | ||
|
||
| result = pd.read_excel(reader, index_col=0, header=[0, 1]) | ||
|
|
||
| original_values = df.to_numpy() | ||
| result_values = result.to_numpy() | ||
| tm.assert_numpy_array_equal(original_values, result_values) | ||
|
||
|
|
||
| assert result.columns[1][1] != result.columns[2][1] | ||
|
|
||
|
|
||
| @td.skip_if_no("xlrd") | ||
| @td.skip_if_no("openpyxl") | ||
|
|
||
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.
Test all excel engines.