From a38667e856c221026fe91c6821ff6d58a86403c2 Mon Sep 17 00:00:00 2001 From: phofl Date: Sun, 5 Dec 2021 00:30:31 +0100 Subject: [PATCH 1/2] BUG: MultiIndex union setting wrong sortorder parametre --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/core/indexes/multi.py | 2 +- pandas/tests/indexing/multiindex/test_loc.py | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 12b44a2bcbd5a..161d00b44c670 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -699,6 +699,7 @@ MultiIndex - Bug in :meth:`MultiIndex.get_loc` where the first level is a :class:`DatetimeIndex` and a string key is passed (:issue:`42465`) - Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`) - Bug in :meth:`MultiIndex.get_loc` raising ``TypeError`` instead of ``KeyError`` on nested tuple (:issue:`42440`) +- Bug in :meth:`MultiIndex.union` setting wrong ``sortorder`` causing errors in subsequent indexing operations with slices (:issue:`44752`) - Bug in :meth:`MultiIndex.putmask` where the other value was also a :class:`MultiIndex` (:issue:`43212`) - diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 53d584f801b0f..88b37ffaa9493 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -3593,7 +3593,7 @@ def _union(self, other, sort) -> MultiIndex: rvals = other._values.astype(object, copy=False) result = lib.fast_unique_multiple([self._values, rvals], sort=sort) - return MultiIndex.from_arrays(zip(*result), sortorder=0, names=result_names) + return MultiIndex.from_arrays(zip(*result), sortorder=None, names=result_names) def _is_comparable_dtype(self, dtype: DtypeObj) -> bool: return is_object_dtype(dtype) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index d036773c778e6..899e4e5d60ab3 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -379,6 +379,22 @@ def test_multiindex_setitem_columns_enlarging(self, indexer, exp_value): ) tm.assert_frame_equal(df, expected) + def test_sorted_multiindex_after_union(self): + # GH#44752 + midx = MultiIndex.from_product( + [pd.date_range("20110101", periods=2), Index(["a", "b"])] + ) + ser1 = Series(1, index=midx) + ser2 = Series(1, index=midx[:2]) + df = pd.concat([ser1, ser2], axis=1) + expected = df.copy() + result = df.loc["2011-01-01":"2011-01-02"] + tm.assert_frame_equal(result, expected) + + df = DataFrame({0: ser1, 1: ser2}) + result = df.loc["2011-01-01":"2011-01-02"] + tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize( "indexer, pos", From 3b8a78a602eef0b6c1f36cf1efa3aa3ae73c504c Mon Sep 17 00:00:00 2001 From: phofl Date: Sun, 5 Dec 2021 00:51:06 +0100 Subject: [PATCH 2/2] Add test --- pandas/tests/indexing/multiindex/test_loc.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 899e4e5d60ab3..1756cc3ae707c 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -395,6 +395,10 @@ def test_sorted_multiindex_after_union(self): result = df.loc["2011-01-01":"2011-01-02"] tm.assert_frame_equal(result, expected) + df = pd.concat([ser1, ser2.reindex(ser1.index)], axis=1) + result = df.loc["2011-01-01":"2011-01-02"] + tm.assert_frame_equal(result, expected) + @pytest.mark.parametrize( "indexer, pos",