Skip to content

API: np.isinf on Index return Index[bool] #61874

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
Jul 16, 2025
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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ Other API changes
- Index set operations (like union or intersection) will now ignore the dtype of
an empty ``RangeIndex`` or empty ``Index`` with object dtype when determining
the dtype of the resulting Index (:issue:`60797`)
- 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`)

.. ---------------------------------------------------------------------------
.. _whatsnew_300.deprecations:
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,12 +965,8 @@ def __array_wrap__(self, result, context=None, return_scalar=False):
Gets called after a ufunc and other functions e.g. np.split.
"""
result = lib.item_from_zerodim(result)
if (not isinstance(result, Index) and is_bool_dtype(result.dtype)) or np.ndim(
result
) > 1:
# exclude Index to avoid warning from is_bool_dtype deprecation;
# in the Index case it doesn't matter which path we go down.
# reached in plotting tests with e.g. np.nonzero(index)
if np.ndim(result) > 1:
# Reached in plotting tests with e.g. np.nonzero(index)
return result

return Index(result, name=self.name)
Expand Down
14 changes: 7 additions & 7 deletions pandas/tests/indexes/test_numpy_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from pandas import (
BooleanDtype,
CategoricalIndex,
DatetimeIndex,
Index,
Expand All @@ -14,7 +15,6 @@
is_complex_dtype,
is_numeric_dtype,
)
from pandas.core.arrays import BooleanArray
from pandas.core.indexes.datetimelike import DatetimeIndexOpsMixin


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

out = np.empty(index.shape, dtype=bool)
func(index, out=out)
tm.assert_numpy_array_equal(out, result)
tm.assert_index_equal(Index(out), result)
else:
with tm.external_error_raised(TypeError):
func(index)
Expand All @@ -129,19 +128,20 @@ def test_numpy_ufuncs_other(index, func):
):
# Results in bool array
result = func(index)
assert isinstance(result, Index)
if not isinstance(index.dtype, np.dtype):
# e.g. Int64 we expect to get BooleanArray back
assert isinstance(result, BooleanArray)
assert isinstance(result.dtype, BooleanDtype)
else:
assert isinstance(result, np.ndarray)
assert isinstance(result.dtype, np.dtype)

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

if not isinstance(index.dtype, np.dtype):
tm.assert_numpy_array_equal(out, result._data)
tm.assert_index_equal(result, Index(out, dtype="boolean"))
else:
tm.assert_numpy_array_equal(out, result)
tm.assert_index_equal(result, Index(out))

elif len(index) == 0:
pass
Expand Down
Loading