Skip to content

Commit 71a327c

Browse files
DOC: docstrings for __array_wrap__ (#35629)
1 parent 188ce73 commit 71a327c

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

pandas/core/generic.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,28 @@ def empty(self) -> bool_t:
17721772
def __array__(self, dtype=None) -> np.ndarray:
17731773
return np.asarray(self._values, dtype=dtype)
17741774

1775-
def __array_wrap__(self, result, context=None):
1775+
def __array_wrap__(
1776+
self,
1777+
result: np.ndarray,
1778+
context: Optional[Tuple[Callable, Tuple[Any, ...], int]] = None,
1779+
):
1780+
"""
1781+
Gets called after a ufunc and other functions.
1782+
1783+
Parameters
1784+
----------
1785+
result: np.ndarray
1786+
The result of the ufunc or other function called on the NumPy array
1787+
returned by __array__
1788+
context: tuple of (func, tuple, int)
1789+
This parameter is returned by ufuncs as a 3-element tuple: (name of the
1790+
ufunc, arguments of the ufunc, domain of the ufunc), but is not set by
1791+
other numpy functions.q
1792+
1793+
Notes
1794+
-----
1795+
Series implements __array_ufunc_ so this not called for ufunc on Series.
1796+
"""
17761797
result = lib.item_from_zerodim(result)
17771798
if is_scalar(result):
17781799
# e.g. we get here with np.ptp(series)

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ def __array__(self, dtype=None) -> np.ndarray:
574574

575575
def __array_wrap__(self, result, context=None):
576576
"""
577-
Gets called after a ufunc.
577+
Gets called after a ufunc and other functions.
578578
"""
579579
result = lib.item_from_zerodim(result)
580580
if is_bool_dtype(result) or lib.is_scalar(result) or np.ndim(result) > 1:

pandas/core/indexes/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def values(self):
116116

117117
def __array_wrap__(self, result, context=None):
118118
"""
119-
Gets called after a ufunc.
119+
Gets called after a ufunc and other functions.
120120
"""
121121
result = lib.item_from_zerodim(result)
122122
if is_bool_dtype(result) or lib.is_scalar(result):

pandas/core/indexes/period.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,13 @@ def _int64index(self) -> Int64Index:
345345

346346
def __array_wrap__(self, result, context=None):
347347
"""
348-
Gets called after a ufunc. Needs additional handling as
349-
PeriodIndex stores internal data as int dtype
348+
Gets called after a ufunc and other functions.
350349
351-
Replace this to __numpy_ufunc__ in future version
350+
Needs additional handling as PeriodIndex stores internal data as int
351+
dtype
352+
353+
Replace this to __numpy_ufunc__ in future version and implement
354+
__array_function__ for Indexes
352355
"""
353356
if isinstance(context, tuple) and len(context) > 0:
354357
func = context[0]

0 commit comments

Comments
 (0)