From 5443f172bdedf4a6eac868b586781044a48a8427 Mon Sep 17 00:00:00 2001 From: phofl Date: Fri, 11 Mar 2022 17:44:07 +0100 Subject: [PATCH] BUG: Index.reindex raising with level and no MultiIndex --- doc/source/whatsnew/v1.5.0.rst | 1 + pandas/core/indexes/base.py | 4 +++- pandas/tests/indexes/multi/test_reindex.py | 3 --- pandas/tests/indexes/test_base.py | 8 ++++++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index dfa87e3cd4574..67dd0365e66d9 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -384,6 +384,7 @@ Indexing - 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`) - Bug in :meth:`DataFrame.where` with multiple columns with datetime-like dtypes failing to downcast results consistent with other dtypes (:issue:`45837`) - Bug in :meth:`Series.loc.__setitem__` and :meth:`Series.loc.__getitem__` not raising when using multiple keys without using a :class:`MultiIndex` (:issue:`13831`) +- Bug in :meth:`Index.reindex` raising ``AssertionError`` when ``level`` was specified but no :class:`MultiIndex` was given; level is ignored now (:issue:`35132`) - Bug when setting a value too large for a :class:`Series` dtype failing to coerce to a common type (:issue:`26049`, :issue:`32878`) - Bug in :meth:`loc.__setitem__` treating ``range`` keys as positional instead of label-based (:issue:`45479`) - Bug in :meth:`Series.__setitem__` when setting ``boolean`` dtype values containing ``NA`` incorrectly raising instead of casting to ``boolean`` dtype (:issue:`45462`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index d717e5cfb1083..c673848bc022a 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -4354,7 +4354,9 @@ def reindex( else: target = ensure_index(target) - if level is not None: + if level is not None and ( + isinstance(self, ABCMultiIndex) or isinstance(target, ABCMultiIndex) + ): if method is not None: raise TypeError("Fill method not supported if level passed") diff --git a/pandas/tests/indexes/multi/test_reindex.py b/pandas/tests/indexes/multi/test_reindex.py index 8136169aa26f6..5d124c19cd865 100644 --- a/pandas/tests/indexes/multi/test_reindex.py +++ b/pandas/tests/indexes/multi/test_reindex.py @@ -42,9 +42,6 @@ def test_reindex_level(idx): with pytest.raises(TypeError, match="Fill method not supported"): idx.reindex(idx, method="pad", level="second") - with pytest.raises(TypeError, match="Fill method not supported"): - index.reindex(index, method="bfill", level="first") - def test_reindex_preserves_names_when_target_is_list_or_ndarray(idx): # GH6552 diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 71a1f926d64cb..1bbbf39505863 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1110,6 +1110,14 @@ def test_reindex_no_type_preserve_target_empty_mi(self): assert result.levels[0].dtype.type == np.int64 assert result.levels[1].dtype.type == np.float64 + def test_reindex_ignoring_level(self): + # GH#35132 + idx = Index([1, 2, 3], name="x") + idx2 = Index([1, 2, 3, 4], name="x") + expected = Index([1, 2, 3, 4], name="x") + result, _ = idx.reindex(idx2, level="x") + tm.assert_index_equal(result, expected) + def test_groupby(self): index = Index(range(5)) result = index.groupby(np.array([1, 1, 2, 2, 2]))