Skip to content

Commit caab88b

Browse files
authored
DEPR: Deprecate empty bool indexer for Index (#56055)
* DEP: Deprecate empty bool indexer for Index * Fix
1 parent 8438fe7 commit caab88b

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

doc/source/whatsnew/v2.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ Other Deprecations
289289
- 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`)
290290
- 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`)
291291
- 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`)
292+
- Deprecated indexing an :class:`Index` with a boolean indexer of length zero (:issue:`55820`)
292293
- 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`)
293294
- 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`)
294295
- 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`)

pandas/core/indexes/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5352,6 +5352,16 @@ def __getitem__(self, key):
53525352
else:
53535353
key = np.asarray(key, dtype=bool)
53545354

5355+
if not isinstance(self.dtype, ExtensionDtype):
5356+
if len(key) == 0 and len(key) != len(self):
5357+
warnings.warn(
5358+
"Using a boolean indexer with length 0 on an Index with "
5359+
"length greater than 0 is deprecated and will raise in a "
5360+
"future version.",
5361+
FutureWarning,
5362+
stacklevel=find_stack_level(),
5363+
)
5364+
53555365
result = getitem(key)
53565366
# Because we ruled out integer above, we always get an arraylike here
53575367
if result.ndim > 1:

pandas/tests/indexes/test_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,11 @@ def test_empty_fancy(self, index, dtype):
462462
empty_index = type(index)([], dtype=index.dtype)
463463

464464
assert index[[]].identical(empty_index)
465-
assert index[empty_arr].identical(empty_index)
465+
if dtype == np.bool_:
466+
with tm.assert_produces_warning(FutureWarning, match="is deprecated"):
467+
assert index[empty_arr].identical(empty_index)
468+
else:
469+
assert index[empty_arr].identical(empty_index)
466470

467471
@pytest.mark.parametrize(
468472
"index",

pandas/tests/internals/test_internals.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,6 @@ def assert_slice_ok(mgr, axis, slobj):
991991
# 2D only support slice objects
992992

993993
# boolean mask
994-
assert_slice_ok(mgr, ax, np.array([], dtype=np.bool_))
995994
assert_slice_ok(mgr, ax, np.ones(mgr.shape[ax], dtype=np.bool_))
996995
assert_slice_ok(mgr, ax, np.zeros(mgr.shape[ax], dtype=np.bool_))
997996

0 commit comments

Comments
 (0)