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: allow None as name in multi-index during join #59546

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ Groupby/resample/rolling

Reshaping
^^^^^^^^^
- :attr:`MultiIndex.names` containing ``None`` no longer throws ``AssertionError`` during join (:issue:`58721`)
matiaslindgren marked this conversation as resolved.
Show resolved Hide resolved
- Bug in :func:`qcut` where values at the quantile boundaries could be incorrectly assigned (:issue:`59355`)
- Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
- Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4516,8 +4516,8 @@ def _join_multi(self, other: Index, how: JoinHow):
from pandas.core.reshape.merge import restore_dropped_levels_multijoin

# figure out join names
self_names_list = list(com.not_none(*self.names))
other_names_list = list(com.not_none(*other.names))
self_names_list = list(self.names)
other_names_list = list(other.names)
self_names_order = self_names_list.index
other_names_order = other_names_list.index
self_names = set(self_names_list)
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -1098,3 +1098,29 @@ def test_join_multiindex_categorical_output_index_dtype(how, values):

result = df1.join(df2, how=how)
tm.assert_frame_equal(result, expected)


def test_join_multiindex_with_none_as_label():
# GH 58721
df1 = DataFrame(
{"A": [1]},
index=MultiIndex.from_tuples([(3, 3)], names=["X", None]),
)
df2 = DataFrame(
{"B": [2]},
index=MultiIndex.from_tuples([(3, 3)], names=[None, "X"]),
)

result12 = df1.join(df2)
expected12 = DataFrame(
{"A": [1], "B": [2]},
index=MultiIndex.from_tuples([(3, 3)], names=["X", None]),
)
tm.assert_frame_equal(result12, expected12)

result21 = df2.join(df1)
expected21 = DataFrame(
{"B": [2], "A": [1]},
index=MultiIndex.from_tuples([(3, 3)], names=[None, "X"]),
)
tm.assert_frame_equal(result21, expected21)
Loading