Skip to content

Commit 99332e4

Browse files
committed
📝 Update the pandas.DataFrame.abs function documentation.
Improve description and returns, and added notes and examples.
1 parent 840d432 commit 99332e4

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

pandas/core/generic.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7115,12 +7115,61 @@ def _tz_localize(ax, tz, ambiguous):
71157115
# Numeric Methods
71167116
def abs(self):
71177117
"""
7118-
Return an object with absolute value taken--only applicable to objects
7119-
that are all numeric.
7118+
Return a Series/DataFrame with absolute numeric value of each object.
7119+
7120+
This function only applies to objects that are all numeric.
71207121
71217122
Returns
71227123
-------
7123-
abs: type of caller
7124+
abs
7125+
Series/DataFrame containing the absolute value of each object.
7126+
7127+
Notes
7128+
-----
7129+
For ``complex`` inputs, ``1.2 + 1j``, the absolute value is
7130+
:math:`\\sqrt{ a^2 + b^2 }`. See the Python
7131+
7132+
Examples
7133+
--------
7134+
Absolute numeric values in a ``Series``.
7135+
7136+
>>> s = pd.Series([-1.10, 2, -3.33, 4])
7137+
>>> s.abs()
7138+
0 1.10
7139+
1 2.00
7140+
2 3.33
7141+
3 4.00
7142+
dtype: float64
7143+
7144+
Absolute numeric values in a ``Series`` with ``complex`` numbers.
7145+
7146+
>>> s = pd.Series([1.2 + 1j])
7147+
>>> s.abs()
7148+
0 1.56205
7149+
dtype: float64
7150+
7151+
Select rows with data closest to certian value using argsort (from
7152+
`StackOverflow
7153+
<http://stackoverflow.com/questions/17758023/return-rows-in-a-dataframe-closest-to-a-user-defined-number>`__).
7154+
7155+
>>> df = pd.DataFrame({
7156+
... 'a': [4, 5, 6, 7],
7157+
... 'b': [10,20,30,40],
7158+
... 'c': [100,50,-30,-50]
7159+
... })
7160+
>>> df
7161+
a b c
7162+
0 4 10 100
7163+
1 5 20 50
7164+
2 6 30 -30
7165+
3 7 40 -50
7166+
>>> a_value = 43.0
7167+
>>> df.loc[(df.c - a_value).abs().argsort()]
7168+
a b c
7169+
1 5 20 50
7170+
0 4 10 100
7171+
2 6 30 -30
7172+
3 7 40 -50
71247173
"""
71257174
return np.abs(self)
71267175

0 commit comments

Comments
 (0)