Skip to content

Commit 43711d5

Browse files
authored
API: np.isinf on Index return Index[bool] (#61874)
1 parent 1d153bb commit 43711d5

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ Other API changes
414414
- Index set operations (like union or intersection) will now ignore the dtype of
415415
an empty ``RangeIndex`` or empty ``Index`` with object dtype when determining
416416
the dtype of the resulting Index (:issue:`60797`)
417+
- Numpy functions like ``np.isinf`` that return a bool dtype when called on a :class:`Index` object now return a bool-dtype :class:`Index` instead of ``np.ndarray`` (:issue:`52676`)
417418

418419
.. ---------------------------------------------------------------------------
419420
.. _whatsnew_300.deprecations:

pandas/core/indexes/base.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -965,12 +965,8 @@ def __array_wrap__(self, result, context=None, return_scalar=False):
965965
Gets called after a ufunc and other functions e.g. np.split.
966966
"""
967967
result = lib.item_from_zerodim(result)
968-
if (not isinstance(result, Index) and is_bool_dtype(result.dtype)) or np.ndim(
969-
result
970-
) > 1:
971-
# exclude Index to avoid warning from is_bool_dtype deprecation;
972-
# in the Index case it doesn't matter which path we go down.
973-
# reached in plotting tests with e.g. np.nonzero(index)
968+
if np.ndim(result) > 1:
969+
# Reached in plotting tests with e.g. np.nonzero(index)
974970
return result
975971

976972
return Index(result, name=self.name)

pandas/tests/indexes/test_numpy_compat.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
from pandas import (
5+
BooleanDtype,
56
CategoricalIndex,
67
DatetimeIndex,
78
Index,
@@ -14,7 +15,6 @@
1415
is_complex_dtype,
1516
is_numeric_dtype,
1617
)
17-
from pandas.core.arrays import BooleanArray
1818
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin
1919

2020

@@ -111,11 +111,10 @@ def test_numpy_ufuncs_other(index, func):
111111
if func in (np.isfinite, np.isinf, np.isnan):
112112
# numpy 1.18 changed isinf and isnan to not raise on dt64/td64
113113
result = func(index)
114-
assert isinstance(result, np.ndarray)
115114

116115
out = np.empty(index.shape, dtype=bool)
117116
func(index, out=out)
118-
tm.assert_numpy_array_equal(out, result)
117+
tm.assert_index_equal(Index(out), result)
119118
else:
120119
with tm.external_error_raised(TypeError):
121120
func(index)
@@ -129,19 +128,20 @@ def test_numpy_ufuncs_other(index, func):
129128
):
130129
# Results in bool array
131130
result = func(index)
131+
assert isinstance(result, Index)
132132
if not isinstance(index.dtype, np.dtype):
133133
# e.g. Int64 we expect to get BooleanArray back
134-
assert isinstance(result, BooleanArray)
134+
assert isinstance(result.dtype, BooleanDtype)
135135
else:
136-
assert isinstance(result, np.ndarray)
136+
assert isinstance(result.dtype, np.dtype)
137137

138138
out = np.empty(index.shape, dtype=bool)
139139
func(index, out=out)
140140

141141
if not isinstance(index.dtype, np.dtype):
142-
tm.assert_numpy_array_equal(out, result._data)
142+
tm.assert_index_equal(result, Index(out, dtype="boolean"))
143143
else:
144-
tm.assert_numpy_array_equal(out, result)
144+
tm.assert_index_equal(result, Index(out))
145145

146146
elif len(index) == 0:
147147
pass

0 commit comments

Comments
 (0)