Skip to content

Commit 914d4f6

Browse files
jbrockmendelpull[bot]
authored andcommitted
DEPR: Index.__getitem__[bool] GH#44051 (#44973)
1 parent d908283 commit 914d4f6

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ Other Deprecations
540540
- Deprecated parameter ``names`` in :meth:`Index.copy` (:issue:`44916`)
541541
- A deprecation warning is now shown for :meth:`DataFrame.to_latex` indicating the arguments signature may change and emulate more the arguments to :meth:`.Styler.to_latex` in future versions (:issue:`44411`)
542542
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
543+
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
543544
-
544545

545546
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4940,7 +4940,8 @@ def __getitem__(self, key):
49404940
"""
49414941
getitem = self._data.__getitem__
49424942

4943-
if is_scalar(key):
4943+
if is_integer(key) or is_float(key):
4944+
# GH#44051 exclude bool, which would return a 2d ndarray
49444945
key = com.cast_scalar_indexer(key, warn_float=True)
49454946
return getitem(key)
49464947

pandas/tests/indexes/common.py

+15
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,21 @@ def test_getitem_2d_deprecated(self, simple_index):
670670

671671
assert isinstance(res, np.ndarray), type(res)
672672

673+
if not isinstance(idx, RangeIndex):
674+
# GH#44051 RangeIndex already raises
675+
with tm.assert_produces_warning(FutureWarning, match=msg):
676+
res = idx[True]
677+
assert isinstance(res, np.ndarray), type(res)
678+
with tm.assert_produces_warning(FutureWarning, match=msg):
679+
res = idx[False]
680+
assert isinstance(res, np.ndarray), type(res)
681+
else:
682+
msg = "only integers, slices"
683+
with pytest.raises(IndexError, match=msg):
684+
idx[True]
685+
with pytest.raises(IndexError, match=msg):
686+
idx[False]
687+
673688
def test_copy_shares_cache(self, simple_index):
674689
# GH32898, GH36840
675690
idx = simple_index

pandas/tests/indexes/interval/test_base.py

+6
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ def test_getitem_2d_deprecated(self, simple_index):
6363
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
6464
with tm.assert_produces_warning(FutureWarning):
6565
idx[:, None]
66+
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
67+
# GH#44051
68+
idx[True]
69+
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
70+
# GH#44051
71+
idx[False]

0 commit comments

Comments
 (0)