From caab88b1a3ba5c697bb71129038c90f5b6df6b79 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:37:51 +0100 Subject: [PATCH] DEPR: Deprecate empty bool indexer for Index (#56055) * DEP: Deprecate empty bool indexer for Index * Fix --- doc/source/whatsnew/v2.2.0.rst | 1 + pandas/core/indexes/base.py | 10 ++++++++++ pandas/tests/indexes/test_base.py | 6 +++++- pandas/tests/internals/test_internals.py | 1 - 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v2.2.0.rst b/doc/source/whatsnew/v2.2.0.rst index 9ef4eb5eb22f3..749e3bf9dc373 100644 --- a/doc/source/whatsnew/v2.2.0.rst +++ b/doc/source/whatsnew/v2.2.0.rst @@ -289,6 +289,7 @@ Other Deprecations - Deprecated automatic downcasting of object-dtype results in :meth:`Series.replace` and :meth:`DataFrame.replace`, explicitly call ``result = result.infer_objects(copy=False)`` instead. To opt in to the future version, use ``pd.set_option("future.no_silent_downcasting", True)`` (:issue:`54710`) - Deprecated downcasting behavior in :meth:`Series.where`, :meth:`DataFrame.where`, :meth:`Series.mask`, :meth:`DataFrame.mask`, :meth:`Series.clip`, :meth:`DataFrame.clip`; in a future version these will not infer object-dtype columns to non-object dtype, or all-round floats to integer dtype. Call ``result.infer_objects(copy=False)`` on the result for object inference, or explicitly cast floats to ints. To opt in to the future version, use ``pd.set_option("future.no_silent_downcasting", True)`` (:issue:`53656`) - Deprecated including the groups in computations when using :meth:`DataFrameGroupBy.apply` and :meth:`DataFrameGroupBy.resample`; pass ``include_groups=False`` to exclude the groups (:issue:`7155`) +- Deprecated indexing an :class:`Index` with a boolean indexer of length zero (:issue:`55820`) - Deprecated not passing a tuple to :class:`DataFrameGroupBy.get_group` or :class:`SeriesGroupBy.get_group` when grouping by a length-1 list-like (:issue:`25971`) - Deprecated string ``AS`` denoting frequency in :class:`YearBegin` and strings ``AS-DEC``, ``AS-JAN``, etc. denoting annual frequencies with various fiscal year starts (:issue:`54275`) - Deprecated string ``A`` denoting frequency in :class:`YearEnd` and strings ``A-DEC``, ``A-JAN``, etc. denoting annual frequencies with various fiscal year ends (:issue:`54275`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 1b4e14f075f22..687d6feb74131 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -5352,6 +5352,16 @@ def __getitem__(self, key): else: key = np.asarray(key, dtype=bool) + if not isinstance(self.dtype, ExtensionDtype): + if len(key) == 0 and len(key) != len(self): + warnings.warn( + "Using a boolean indexer with length 0 on an Index with " + "length greater than 0 is deprecated and will raise in a " + "future version.", + FutureWarning, + stacklevel=find_stack_level(), + ) + result = getitem(key) # Because we ruled out integer above, we always get an arraylike here if result.ndim > 1: diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 7620bf272db11..dc624f0271a73 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -462,7 +462,11 @@ def test_empty_fancy(self, index, dtype): empty_index = type(index)([], dtype=index.dtype) assert index[[]].identical(empty_index) - assert index[empty_arr].identical(empty_index) + if dtype == np.bool_: + with tm.assert_produces_warning(FutureWarning, match="is deprecated"): + assert index[empty_arr].identical(empty_index) + else: + assert index[empty_arr].identical(empty_index) @pytest.mark.parametrize( "index", diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index ae79da4bbe0d3..792d1323ec730 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -991,7 +991,6 @@ def assert_slice_ok(mgr, axis, slobj): # 2D only support slice objects # boolean mask - assert_slice_ok(mgr, ax, np.array([], dtype=np.bool_)) assert_slice_ok(mgr, ax, np.ones(mgr.shape[ax], dtype=np.bool_)) assert_slice_ok(mgr, ax, np.zeros(mgr.shape[ax], dtype=np.bool_))