Skip to content

Commit f6053e4

Browse files
jbrockmendelgasparitiago
authored andcommitted
REF: dont use np.abs in NDFrame.__abs__ (pandas-dev#43847)
1 parent c9a09d4 commit f6053e4

File tree

1 file changed

+71
-72
lines changed

1 file changed

+71
-72
lines changed

pandas/core/generic.py

+71-72
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()
@@ -9838,78 +9909,6 @@ def _tz_localize(ax, tz, ambiguous, nonexistent):
98389909
# ----------------------------------------------------------------------
98399910
# Numeric Methods
98409911

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

0 commit comments

Comments
 (0)