Skip to content

Commit b71761d

Browse files
authored
BUG: MultiIndex union setting wrong sortorder parameter (#44762)
1 parent bc17343 commit b71761d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ MultiIndex
714714
- Bug in :meth:`MultiIndex.get_loc` where the first level is a :class:`DatetimeIndex` and a string key is passed (:issue:`42465`)
715715
- Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`)
716716
- Bug in :meth:`MultiIndex.get_loc` raising ``TypeError`` instead of ``KeyError`` on nested tuple (:issue:`42440`)
717+
- Bug in :meth:`MultiIndex.union` setting wrong ``sortorder`` causing errors in subsequent indexing operations with slices (:issue:`44752`)
717718
- Bug in :meth:`MultiIndex.putmask` where the other value was also a :class:`MultiIndex` (:issue:`43212`)
718719
-
719720

pandas/core/indexes/multi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3593,7 +3593,7 @@ def _union(self, other, sort) -> MultiIndex:
35933593
rvals = other._values.astype(object, copy=False)
35943594
result = lib.fast_unique_multiple([self._values, rvals], sort=sort)
35953595

3596-
return MultiIndex.from_arrays(zip(*result), sortorder=0, names=result_names)
3596+
return MultiIndex.from_arrays(zip(*result), sortorder=None, names=result_names)
35973597

35983598
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
35993599
return is_object_dtype(dtype)

pandas/tests/indexing/multiindex/test_loc.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,26 @@ def test_multiindex_setitem_columns_enlarging(self, indexer, exp_value):
379379
)
380380
tm.assert_frame_equal(df, expected)
381381

382+
def test_sorted_multiindex_after_union(self):
383+
# GH#44752
384+
midx = MultiIndex.from_product(
385+
[pd.date_range("20110101", periods=2), Index(["a", "b"])]
386+
)
387+
ser1 = Series(1, index=midx)
388+
ser2 = Series(1, index=midx[:2])
389+
df = pd.concat([ser1, ser2], axis=1)
390+
expected = df.copy()
391+
result = df.loc["2011-01-01":"2011-01-02"]
392+
tm.assert_frame_equal(result, expected)
393+
394+
df = DataFrame({0: ser1, 1: ser2})
395+
result = df.loc["2011-01-01":"2011-01-02"]
396+
tm.assert_frame_equal(result, expected)
397+
398+
df = pd.concat([ser1, ser2.reindex(ser1.index)], axis=1)
399+
result = df.loc["2011-01-01":"2011-01-02"]
400+
tm.assert_frame_equal(result, expected)
401+
382402

383403
@pytest.mark.parametrize(
384404
"indexer, pos",

0 commit comments

Comments
 (0)