Skip to content

REGR: np.argwhere on pd.Series raises ValueError #35334

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

Closed
Closed
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
42 changes: 37 additions & 5 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1777,16 +1777,48 @@ def empty(self) -> bool_t:
def __array__(self, dtype=None) -> np.ndarray:
return np.asarray(self._values, dtype=dtype)

def __array_wrap__(self, result, context=None):
def __array_wrap__(
self,
result: np.ndarray,
context: Optional[Tuple[Callable, Tuple[Any, ...], int]] = None,
):
"""
Gets called after a ufunc and other functions.

Parameters
----------
result: np.ndarray
The result of the ufunc or other function called on the NumPy array
returned by __array__
context: tuple of (func, tuple, int)
This parameter is returned by ufuncs as a 3-element tuple: (name of the
ufunc, arguments of the ufunc, domain of the ufunc), but is not set by
other numpy functions.q

Notes
-----
Series implements __array_ufunc_ so this not called for ufunc on Series.
"""
result = lib.item_from_zerodim(result)
if is_scalar(result):
# e.g. we get here with np.ptp(series)
# ptp also requires the item_from_zerodim
return result
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
return self._constructor(result, **d).__finalize__(
self, method="__array_wrap__"
)

# if shape of result is the same as self we use the original axes
if result.shape == self.shape:
d = self._construct_axes_dict(self._AXIS_ORDERS, copy=False)
else:
d = dict()

# if ndim of result is the same as self we return the same object
# otherwise we just return the NumPy array
if result.ndim == self.ndim:
return self._constructor(result, **d).__finalize__(
self, method="__array_wrap__"
)
else:
return result

# ideally we would define this to avoid the getattr checks, but
# is slower
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def __array__(self, dtype=None) -> np.ndarray:

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc.
Gets called after a ufunc and other functions.
"""
result = lib.item_from_zerodim(result)
if is_bool_dtype(result) or lib.is_scalar(result) or np.ndim(result) > 1:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def values(self):

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc.
Gets called after a ufunc and other functions.
"""
result = lib.item_from_zerodim(result)
if is_bool_dtype(result) or lib.is_scalar(result):
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,13 @@ def _int64index(self) -> Int64Index:

def __array_wrap__(self, result, context=None):
"""
Gets called after a ufunc. Needs additional handling as
PeriodIndex stores internal data as int dtype
Gets called after a ufunc and other functions.

Replace this to __numpy_ufunc__ in future version
Needs additional handling as PeriodIndex stores internal data as int
dtype

Replace this to __numpy_ufunc__ in future version and implement
__array_function__ for Indexes
"""
if isinstance(context, tuple) and len(context) > 0:
func = context[0]
Expand Down