Skip to content

Commit bd95c6e

Browse files
authored
REF: dont use np.abs in NDFrame.__abs__ (#43847)
1 parent 927d4ec commit bd95c6e

File tree

1 file changed

+71
-72
lines changed

1 file changed

+71
-72
lines changed

pandas/core/generic.py

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,77 @@ def bool(self):
15891589

15901590
self.__nonzero__()
15911591

1592+
@final
1593+
def abs(self: NDFrameT) -> NDFrameT:
1594+
"""
1595+
Return a Series/DataFrame with absolute numeric value of each element.
1596+
1597+
This function only applies to elements that are all numeric.
1598+
1599+
Returns
1600+
-------
1601+
abs
1602+
Series/DataFrame containing the absolute value of each element.
1603+
1604+
See Also
1605+
--------
1606+
numpy.absolute : Calculate the absolute value element-wise.
1607+
1608+
Notes
1609+
-----
1610+
For ``complex`` inputs, ``1.2 + 1j``, the absolute value is
1611+
:math:`\\sqrt{ a^2 + b^2 }`.
1612+
1613+
Examples
1614+
--------
1615+
Absolute numeric values in a Series.
1616+
1617+
>>> s = pd.Series([-1.10, 2, -3.33, 4])
1618+
>>> s.abs()
1619+
0 1.10
1620+
1 2.00
1621+
2 3.33
1622+
3 4.00
1623+
dtype: float64
1624+
1625+
Absolute numeric values in a Series with complex numbers.
1626+
1627+
>>> s = pd.Series([1.2 + 1j])
1628+
>>> s.abs()
1629+
0 1.56205
1630+
dtype: float64
1631+
1632+
Absolute numeric values in a Series with a Timedelta element.
1633+
1634+
>>> s = pd.Series([pd.Timedelta('1 days')])
1635+
>>> s.abs()
1636+
0 1 days
1637+
dtype: timedelta64[ns]
1638+
1639+
Select rows with data closest to certain value using argsort (from
1640+
`StackOverflow <https://stackoverflow.com/a/17758115>`__).
1641+
1642+
>>> df = pd.DataFrame({
1643+
... 'a': [4, 5, 6, 7],
1644+
... 'b': [10, 20, 30, 40],
1645+
... 'c': [100, 50, -30, -50]
1646+
... })
1647+
>>> df
1648+
a b c
1649+
0 4 10 100
1650+
1 5 20 50
1651+
2 6 30 -30
1652+
3 7 40 -50
1653+
>>> df.loc[(df.c - 43).abs().argsort()]
1654+
a b c
1655+
1 5 20 50
1656+
0 4 10 100
1657+
2 6 30 -30
1658+
3 7 40 -50
1659+
"""
1660+
res_mgr = self._mgr.apply(np.abs)
1661+
return self._constructor(res_mgr).__finalize__(self, name="abs")
1662+
15921663
@final
15931664
def __abs__(self: NDFrameT) -> NDFrameT:
15941665
return self.abs()
@@ -9822,78 +9893,6 @@ def _tz_localize(ax, tz, ambiguous, nonexistent):
98229893
# ----------------------------------------------------------------------
98239894
# Numeric Methods
98249895

9825-
@final
9826-
def abs(self: NDFrameT) -> NDFrameT:
9827-
"""
9828-
Return a Series/DataFrame with absolute numeric value of each element.
9829-
9830-
This function only applies to elements that are all numeric.
9831-
9832-
Returns
9833-
-------
9834-
abs
9835-
Series/DataFrame containing the absolute value of each element.
9836-
9837-
See Also
9838-
--------
9839-
numpy.absolute : Calculate the absolute value element-wise.
9840-
9841-
Notes
9842-
-----
9843-
For ``complex`` inputs, ``1.2 + 1j``, the absolute value is
9844-
:math:`\\sqrt{ a^2 + b^2 }`.
9845-
9846-
Examples
9847-
--------
9848-
Absolute numeric values in a Series.
9849-
9850-
>>> s = pd.Series([-1.10, 2, -3.33, 4])
9851-
>>> s.abs()
9852-
0 1.10
9853-
1 2.00
9854-
2 3.33
9855-
3 4.00
9856-
dtype: float64
9857-
9858-
Absolute numeric values in a Series with complex numbers.
9859-
9860-
>>> s = pd.Series([1.2 + 1j])
9861-
>>> s.abs()
9862-
0 1.56205
9863-
dtype: float64
9864-
9865-
Absolute numeric values in a Series with a Timedelta element.
9866-
9867-
>>> s = pd.Series([pd.Timedelta('1 days')])
9868-
>>> s.abs()
9869-
0 1 days
9870-
dtype: timedelta64[ns]
9871-
9872-
Select rows with data closest to certain value using argsort (from
9873-
`StackOverflow <https://stackoverflow.com/a/17758115>`__).
9874-
9875-
>>> df = pd.DataFrame({
9876-
... 'a': [4, 5, 6, 7],
9877-
... 'b': [10, 20, 30, 40],
9878-
... 'c': [100, 50, -30, -50]
9879-
... })
9880-
>>> df
9881-
a b c
9882-
0 4 10 100
9883-
1 5 20 50
9884-
2 6 30 -30
9885-
3 7 40 -50
9886-
>>> df.loc[(df.c - 43).abs().argsort()]
9887-
a b c
9888-
1 5 20 50
9889-
0 4 10 100
9890-
2 6 30 -30
9891-
3 7 40 -50
9892-
"""
9893-
# error: Incompatible return value type (got "ndarray[Any, dtype[Any]]",
9894-
# expected "NDFrameT")
9895-
return np.abs(self) # type: ignore[return-value]
9896-
98979896
@final
98989897
def describe(
98999898
self: NDFrameT,

0 commit comments

Comments
 (0)