Skip to content

Commit be81ae0

Browse files
authored
BUG: Index.reindex raising with level and no MultiIndex (#46327)
1 parent c832ccb commit be81ae0

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ Indexing
384384
- Bug in :meth:`Series.__setitem__` when setting incompatible values into a ``PeriodDtype`` or ``IntervalDtype`` :class:`Series` raising when indexing with a boolean mask but coercing when indexing with otherwise-equivalent indexers; these now consistently coerce, along with :meth:`Series.mask` and :meth:`Series.where` (:issue:`45768`)
385385
- Bug in :meth:`DataFrame.where` with multiple columns with datetime-like dtypes failing to downcast results consistent with other dtypes (:issue:`45837`)
386386
- Bug in :meth:`Series.loc.__setitem__` and :meth:`Series.loc.__getitem__` not raising when using multiple keys without using a :class:`MultiIndex` (:issue:`13831`)
387+
- Bug in :meth:`Index.reindex` raising ``AssertionError`` when ``level`` was specified but no :class:`MultiIndex` was given; level is ignored now (:issue:`35132`)
387388
- Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`)
388389
- Bug in :meth:`loc.__setitem__` treating ``range`` keys as positional instead of label-based (:issue:`45479`)
389390
- Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`)

pandas/core/indexes/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4354,7 +4354,9 @@ def reindex(
43544354
else:
43554355
target = ensure_index(target)
43564356

4357-
if level is not None:
4357+
if level is not None and (
4358+
isinstance(self, ABCMultiIndex) or isinstance(target, ABCMultiIndex)
4359+
):
43584360
if method is not None:
43594361
raise TypeError("Fill method not supported if level passed")
43604362

pandas/tests/indexes/multi/test_reindex.py

-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ def test_reindex_level(idx):
4242
with pytest.raises(TypeError, match="Fill method not supported"):
4343
idx.reindex(idx, method="pad", level="second")
4444

45-
with pytest.raises(TypeError, match="Fill method not supported"):
46-
index.reindex(index, method="bfill", level="first")
47-
4845

4946
def test_reindex_preserves_names_when_target_is_list_or_ndarray(idx):
5047
# GH6552

pandas/tests/indexes/test_base.py

+8
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,14 @@ def test_reindex_no_type_preserve_target_empty_mi(self):
11101110
assert result.levels[0].dtype.type == np.int64
11111111
assert result.levels[1].dtype.type == np.float64
11121112

1113+
def test_reindex_ignoring_level(self):
1114+
# GH#35132
1115+
idx = Index([1, 2, 3], name="x")
1116+
idx2 = Index([1, 2, 3, 4], name="x")
1117+
expected = Index([1, 2, 3, 4], name="x")
1118+
result, _ = idx.reindex(idx2, level="x")
1119+
tm.assert_index_equal(result, expected)
1120+
11131121
def test_groupby(self):
11141122
index = Index(range(5))
11151123
result = index.groupby(np.array([1, 1, 2, 2, 2]))

0 commit comments

Comments
 (0)