Skip to content

DEPR: Index.__getitem__[bool] GH#44051 #44973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ Other Deprecations
- Deprecated parameter ``names`` in :meth:`Index.copy` (:issue:`44916`)
- 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`)
- Deprecated :meth:`Categorical.replace`, use :meth:`Series.replace` instead (:issue:`44929`)
- Deprecated :meth:`Index.__getitem__` with a bool key; use ``index.values[key]`` to get the old behavior (:issue:`44051`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have other deprecated indexing like this? can you co-locate (followup ok too)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think from a previous version, so the note will be in a different file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk that's fine then

-

.. ---------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4940,7 +4940,8 @@ def __getitem__(self, key):
"""
getitem = self._data.__getitem__

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

Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,21 @@ def test_getitem_2d_deprecated(self, simple_index):

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

if not isinstance(idx, RangeIndex):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about a list of bools?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, but definitely out of scope

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk

# GH#44051 RangeIndex already raises
with tm.assert_produces_warning(FutureWarning, match=msg):
res = idx[True]
assert isinstance(res, np.ndarray), type(res)
with tm.assert_produces_warning(FutureWarning, match=msg):
res = idx[False]
assert isinstance(res, np.ndarray), type(res)
else:
msg = "only integers, slices"
with pytest.raises(IndexError, match=msg):
idx[True]
with pytest.raises(IndexError, match=msg):
idx[False]

def test_copy_shares_cache(self, simple_index):
# GH32898, GH36840
idx = simple_index
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/interval/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ def test_getitem_2d_deprecated(self, simple_index):
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
with tm.assert_produces_warning(FutureWarning):
idx[:, None]
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
# GH#44051
idx[True]
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
# GH#44051
idx[False]