Skip to content

BUG: MultiIndex union setting wrong sortorder parameter #44762

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

Merged
merged 4 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,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`)
-

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/indexing/multiindex/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,26 @@ def test_multiindex_setitem_columns_enlarging(self, indexer, exp_value):
)
tm.assert_frame_equal(df, expected)

def test_sorted_multiindex_after_union(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are all 3 cases from the OP represented (elsewhere)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, added the working case too

# 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)

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",
Expand Down