Skip to content

Commit 8f7080b

Browse files
matiaslindgrenmroeschkepre-commit-ci[bot]
authored
BUG: allow None as name in multi-index during join (#59546)
* allow None as name in multi-index * update whatsnew * add unit test for none label joins * move bugfix note under Reshaping * Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 15e9e7a commit 8f7080b

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ Reshaping
659659
^^^^^^^^^
660660
- Bug in :func:`qcut` where values at the quantile boundaries could be incorrectly assigned (:issue:`59355`)
661661
- Bug in :meth:`DataFrame.join` inconsistently setting result index name (:issue:`55815`)
662+
- Bug in :meth:`DataFrame.join` when a :class:`DataFrame` with a :class:`MultiIndex` would raise an ``AssertionError`` when :attr:`MultiIndex.names` contained ``None``. (:issue:`58721`)
662663
- Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`)
663664
- Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`)
664665
- Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)

pandas/core/indexes/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4516,8 +4516,8 @@ def _join_multi(self, other: Index, how: JoinHow):
45164516
from pandas.core.reshape.merge import restore_dropped_levels_multijoin
45174517

45184518
# figure out join names
4519-
self_names_list = list(com.not_none(*self.names))
4520-
other_names_list = list(com.not_none(*other.names))
4519+
self_names_list = list(self.names)
4520+
other_names_list = list(other.names)
45214521
self_names_order = self_names_list.index
45224522
other_names_order = other_names_list.index
45234523
self_names = set(self_names_list)

pandas/tests/reshape/merge/test_join.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,3 +1098,29 @@ def test_join_multiindex_categorical_output_index_dtype(how, values):
10981098

10991099
result = df1.join(df2, how=how)
11001100
tm.assert_frame_equal(result, expected)
1101+
1102+
1103+
def test_join_multiindex_with_none_as_label():
1104+
# GH 58721
1105+
df1 = DataFrame(
1106+
{"A": [1]},
1107+
index=MultiIndex.from_tuples([(3, 3)], names=["X", None]),
1108+
)
1109+
df2 = DataFrame(
1110+
{"B": [2]},
1111+
index=MultiIndex.from_tuples([(3, 3)], names=[None, "X"]),
1112+
)
1113+
1114+
result12 = df1.join(df2)
1115+
expected12 = DataFrame(
1116+
{"A": [1], "B": [2]},
1117+
index=MultiIndex.from_tuples([(3, 3)], names=["X", None]),
1118+
)
1119+
tm.assert_frame_equal(result12, expected12)
1120+
1121+
result21 = df2.join(df1)
1122+
expected21 = DataFrame(
1123+
{"B": [2], "A": [1]},
1124+
index=MultiIndex.from_tuples([(3, 3)], names=[None, "X"]),
1125+
)
1126+
tm.assert_frame_equal(result21, expected21)

0 commit comments

Comments
 (0)