From 929bc8baa9b4a8de61f71121fdffb01726a474aa Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 1 Oct 2021 16:15:19 -0700 Subject: [PATCH] REF: dont use np.abs in NDFrame.__abs__ --- pandas/core/generic.py | 143 ++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 72 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 91a446dd99334..9552f467b6132 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1589,6 +1589,77 @@ def bool(self): self.__nonzero__() + @final + def abs(self: NDFrameT) -> NDFrameT: + """ + Return a Series/DataFrame with absolute numeric value of each element. + + This function only applies to elements that are all numeric. + + Returns + ------- + abs + Series/DataFrame containing the absolute value of each element. + + See Also + -------- + numpy.absolute : Calculate the absolute value element-wise. + + Notes + ----- + For ``complex`` inputs, ``1.2 + 1j``, the absolute value is + :math:`\\sqrt{ a^2 + b^2 }`. + + Examples + -------- + Absolute numeric values in a Series. + + >>> s = pd.Series([-1.10, 2, -3.33, 4]) + >>> s.abs() + 0 1.10 + 1 2.00 + 2 3.33 + 3 4.00 + dtype: float64 + + Absolute numeric values in a Series with complex numbers. + + >>> s = pd.Series([1.2 + 1j]) + >>> s.abs() + 0 1.56205 + dtype: float64 + + Absolute numeric values in a Series with a Timedelta element. + + >>> s = pd.Series([pd.Timedelta('1 days')]) + >>> s.abs() + 0 1 days + dtype: timedelta64[ns] + + Select rows with data closest to certain value using argsort (from + `StackOverflow `__). + + >>> df = pd.DataFrame({ + ... 'a': [4, 5, 6, 7], + ... 'b': [10, 20, 30, 40], + ... 'c': [100, 50, -30, -50] + ... }) + >>> df + a b c + 0 4 10 100 + 1 5 20 50 + 2 6 30 -30 + 3 7 40 -50 + >>> df.loc[(df.c - 43).abs().argsort()] + a b c + 1 5 20 50 + 0 4 10 100 + 2 6 30 -30 + 3 7 40 -50 + """ + res_mgr = self._mgr.apply(np.abs) + return self._constructor(res_mgr).__finalize__(self, name="abs") + @final def __abs__(self: NDFrameT) -> NDFrameT: return self.abs() @@ -9822,78 +9893,6 @@ def _tz_localize(ax, tz, ambiguous, nonexistent): # ---------------------------------------------------------------------- # Numeric Methods - @final - def abs(self: NDFrameT) -> NDFrameT: - """ - Return a Series/DataFrame with absolute numeric value of each element. - - This function only applies to elements that are all numeric. - - Returns - ------- - abs - Series/DataFrame containing the absolute value of each element. - - See Also - -------- - numpy.absolute : Calculate the absolute value element-wise. - - Notes - ----- - For ``complex`` inputs, ``1.2 + 1j``, the absolute value is - :math:`\\sqrt{ a^2 + b^2 }`. - - Examples - -------- - Absolute numeric values in a Series. - - >>> s = pd.Series([-1.10, 2, -3.33, 4]) - >>> s.abs() - 0 1.10 - 1 2.00 - 2 3.33 - 3 4.00 - dtype: float64 - - Absolute numeric values in a Series with complex numbers. - - >>> s = pd.Series([1.2 + 1j]) - >>> s.abs() - 0 1.56205 - dtype: float64 - - Absolute numeric values in a Series with a Timedelta element. - - >>> s = pd.Series([pd.Timedelta('1 days')]) - >>> s.abs() - 0 1 days - dtype: timedelta64[ns] - - Select rows with data closest to certain value using argsort (from - `StackOverflow `__). - - >>> df = pd.DataFrame({ - ... 'a': [4, 5, 6, 7], - ... 'b': [10, 20, 30, 40], - ... 'c': [100, 50, -30, -50] - ... }) - >>> df - a b c - 0 4 10 100 - 1 5 20 50 - 2 6 30 -30 - 3 7 40 -50 - >>> df.loc[(df.c - 43).abs().argsort()] - a b c - 1 5 20 50 - 0 4 10 100 - 2 6 30 -30 - 3 7 40 -50 - """ - # error: Incompatible return value type (got "ndarray[Any, dtype[Any]]", - # expected "NDFrameT") - return np.abs(self) # type: ignore[return-value] - @final def describe( self: NDFrameT,