Skip to content

BUG: MultiIndex.reindex with level and EA dtype #42043

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 3 commits into from
Jun 17, 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Missing

MultiIndex
^^^^^^^^^^
-
- Bug in :meth:`MultiIndex.reindex` when passing a ``level`` that corresponds to an ``ExtensionDtype`` level (:issue:`42043`)
-

I/O
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2503,9 +2503,7 @@ def reindex(
target = ibase.ensure_has_len(target)
if len(target) == 0 and not isinstance(target, Index):
idx = self.levels[level]
attrs = idx._get_attributes_dict()
attrs.pop("freq", None) # don't preserve freq
target = type(idx)._simple_new(np.empty(0, dtype=idx.dtype), **attrs)
target = idx[:0]
else:
target = ensure_index(target)
target, indexer, _ = self._join_level(
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/multi/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ def test_reindex_lvl_preserves_type_if_target_is_empty_list_or_array():
assert idx.reindex([], level=0)[0].levels[0].dtype.type == np.int64
assert idx.reindex([], level=1)[0].levels[1].dtype.type == np.object_

# case with EA levels
cat = pd.Categorical(["foo", "bar"])
dti = pd.date_range("2016-01-01", periods=2, tz="US/Pacific")
mi = MultiIndex.from_product([cat, dti])
assert mi.reindex([], level=0)[0].levels[0].dtype == cat.dtype
assert mi.reindex([], level=1)[0].levels[1].dtype == dti.dtype


def test_reindex_base(idx):
idx = idx
Expand Down