From dc44901648c3416e5c40d4bcb7a82b9dc7cd9553 Mon Sep 17 00:00:00 2001 From: Matias Lindgren Date: Sun, 18 Aug 2024 12:37:26 +0200 Subject: [PATCH 1/6] allow None as name in multi-index --- pandas/core/indexes/base.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d39c337fbb4b2..c8dbea1fd39ea 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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) From 0f62df80ecdaf610d8db4fad72dc5b85a5a30395 Mon Sep 17 00:00:00 2001 From: Matias Lindgren Date: Sun, 18 Aug 2024 12:43:05 +0200 Subject: [PATCH 2/6] update whatsnew --- doc/source/whatsnew/v3.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index f25edd39cf7da..3b63e86d7e3f7 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -28,6 +28,7 @@ enhancement2 Other enhancements ^^^^^^^^^^^^^^^^^^ +- :attr:`MultiIndex.names` containing ``None`` no longer throws ``AssertionError`` during join (:issue:`58721`) - :class:`pandas.api.typing.FrozenList` is available for typing the outputs of :attr:`MultiIndex.names`, :attr:`MultiIndex.codes` and :attr:`MultiIndex.levels` (:issue:`58237`) - :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`) - :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`) From f9a552345d8e31e6b35a2ecbfd594be1fdd576ab Mon Sep 17 00:00:00 2001 From: Matias Lindgren Date: Mon, 19 Aug 2024 19:39:57 +0200 Subject: [PATCH 3/6] add unit test for none label joins --- pandas/tests/reshape/merge/test_join.py | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pandas/tests/reshape/merge/test_join.py b/pandas/tests/reshape/merge/test_join.py index f090ded06119a..0f743332acbbe 100644 --- a/pandas/tests/reshape/merge/test_join.py +++ b/pandas/tests/reshape/merge/test_join.py @@ -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) From 67cc4fc8a5882a3a218011b0c9d1787b9a115a85 Mon Sep 17 00:00:00 2001 From: Matias Lindgren Date: Sun, 25 Aug 2024 18:17:54 +0200 Subject: [PATCH 4/6] move bugfix note under Reshaping --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 4d5b9c849d0fd..e7af33521e662 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -28,7 +28,6 @@ enhancement2 Other enhancements ^^^^^^^^^^^^^^^^^^ -- :attr:`MultiIndex.names` containing ``None`` no longer throws ``AssertionError`` during join (:issue:`58721`) - :class:`pandas.api.typing.FrozenList` is available for typing the outputs of :attr:`MultiIndex.names`, :attr:`MultiIndex.codes` and :attr:`MultiIndex.levels` (:issue:`58237`) - :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`) - :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`) @@ -655,6 +654,7 @@ Groupby/resample/rolling Reshaping ^^^^^^^^^ +- :attr:`MultiIndex.names` containing ``None`` no longer throws ``AssertionError`` during join (:issue:`58721`) - 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`) From 72634cc4e7349bd85ea0618cec0fac003e3e1f2a Mon Sep 17 00:00:00 2001 From: matiaslindgren Date: Mon, 26 Aug 2024 19:54:04 +0200 Subject: [PATCH 5/6] Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index e7af33521e662..a5e2cbfa34360 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -654,7 +654,7 @@ Groupby/resample/rolling Reshaping ^^^^^^^^^ -- :attr:`MultiIndex.names` containing ``None`` no longer throws ``AssertionError`` during join (:issue:`58721`) +- 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`) - 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`) From b0449dfb56d45e993032340bbbe45cdec0b3ce75 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 20:56:39 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index a5e2cbfa34360..e801b76cdc204 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -654,9 +654,9 @@ Groupby/resample/rolling Reshaping ^^^^^^^^^ -- 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`) - 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.join` when a :class:`DataFrame` with a :class:`MultiIndex` would raise an ``AssertionError`` when :attr:`MultiIndex.names` contained ``None``. (:issue:`58721`) - Bug in :meth:`DataFrame.merge` where merging on a column containing only ``NaN`` values resulted in an out-of-bounds array access (:issue:`59421`) - Bug in :meth:`DataFrame.unstack` producing incorrect results when ``sort=False`` (:issue:`54987`, :issue:`55516`) - Bug in :meth:`DataFrame.unstack` producing incorrect results when manipulating empty :class:`DataFrame` with an :class:`ExtentionDtype` (:issue:`59123`)