From 99332e4e3aa3ec6ed532fe2ce0906d4fb40d3878 Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sat, 10 Mar 2018 10:39:07 -0500 Subject: [PATCH 1/7] :memo: Update the pandas.DataFrame.abs function documentation. Improve description and returns, and added notes and examples. --- pandas/core/generic.py | 55 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a893b2ba1a189..15327ddc4d564 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7115,12 +7115,61 @@ def _tz_localize(ax, tz, ambiguous): # Numeric Methods def abs(self): """ - Return an object with absolute value taken--only applicable to objects - that are all numeric. + Return a Series/DataFrame with absolute numeric value of each object. + + This function only applies to objects that are all numeric. Returns ------- - abs: type of caller + abs + Series/DataFrame containing the absolute value of each object. + + Notes + ----- + For ``complex`` inputs, ``1.2 + 1j``, the absolute value is + :math:`\\sqrt{ a^2 + b^2 }`. See the Python + + 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 + + Select rows with data closest to certian 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 + >>> a_value = 43.0 + >>> df.loc[(df.c - a_value).abs().argsort()] + a b c + 1 5 20 50 + 0 4 10 100 + 2 6 30 -30 + 3 7 40 -50 """ return np.abs(self) From 7da5bb64d12aaccfb8fdc506498753e4d84a1ad5 Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sat, 10 Mar 2018 11:12:55 -0500 Subject: [PATCH 2/7] :rotating_light: Adding spaces after comma. --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 15327ddc4d564..9040ddf4f8eba 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7154,8 +7154,8 @@ def abs(self): >>> df = pd.DataFrame({ ... 'a': [4, 5, 6, 7], - ... 'b': [10,20,30,40], - ... 'c': [100,50,-30,-50] + ... 'b': [10, 20, 30, 40], + ... 'c': [100, 50, -30, -50] ... }) >>> df a b c From 41658727257c9872fc6d9e2f5d637e4e400c34a3 Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sat, 10 Mar 2018 11:33:27 -0500 Subject: [PATCH 3/7] :bulb: Fix issues with style of docstring. --- pandas/core/generic.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 9040ddf4f8eba..7fe85994f9bd0 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7115,14 +7115,14 @@ def _tz_localize(ax, tz, ambiguous): # Numeric Methods def abs(self): """ - Return a Series/DataFrame with absolute numeric value of each object. + Return a Series/DataFrame with absolute numeric value of each element. - This function only applies to objects that are all numeric. + This function only applies to elements that are all numeric. Returns ------- abs - Series/DataFrame containing the absolute value of each object. + Series/DataFrame containing the absolute value of each element. Notes ----- @@ -7149,8 +7149,7 @@ def abs(self): dtype: float64 Select rows with data closest to certian value using argsort (from - `StackOverflow - `__). + `StackOverflow `__). >>> df = pd.DataFrame({ ... 'a': [4, 5, 6, 7], @@ -7163,8 +7162,7 @@ def abs(self): 1 5 20 50 2 6 30 -30 3 7 40 -50 - >>> a_value = 43.0 - >>> df.loc[(df.c - a_value).abs().argsort()] + >>> df.loc[(df.c - 43).abs().argsort()] a b c 1 5 20 50 0 4 10 100 From 54fffbe5988f4ec3d6e25a9c6325078d666b7d32 Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sat, 10 Mar 2018 12:59:03 -0500 Subject: [PATCH 4/7] :bulb: Add a see also section to the numpy module. --- pandas/core/generic.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 7fe85994f9bd0..5142026d4fc4b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7168,6 +7168,10 @@ def abs(self): 0 4 10 100 2 6 30 -30 3 7 40 -50 + + See Also + -------- + numpy.absolute """ return np.abs(self) From 150f8494092ffcecda7bda7e3c55f5bb670f720a Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sat, 10 Mar 2018 15:33:01 -0500 Subject: [PATCH 5/7] :bulb: Add more information about numpy.absolute. --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5142026d4fc4b..afca683129f67 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7171,7 +7171,7 @@ def abs(self): See Also -------- - numpy.absolute + numpy.absolute : calculate the absolute value element-wise. """ return np.abs(self) From 34296b277a85719e681fe454adfe44950615e216 Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sun, 11 Mar 2018 16:49:37 -0400 Subject: [PATCH 6/7] :bulb: Fixed some style issues. --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index afca683129f67..3878597ddbc18 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7127,11 +7127,11 @@ def abs(self): Notes ----- For ``complex`` inputs, ``1.2 + 1j``, the absolute value is - :math:`\\sqrt{ a^2 + b^2 }`. See the Python + :math:`\\sqrt{ a^2 + b^2 }`. Examples -------- - Absolute numeric values in a ``Series``. + Absolute numeric values in a Series. >>> s = pd.Series([-1.10, 2, -3.33, 4]) >>> s.abs() @@ -7141,7 +7141,7 @@ def abs(self): 3 4.00 dtype: float64 - Absolute numeric values in a ``Series`` with ``complex`` numbers. + Absolute numeric values in a Series with complex numbers. >>> s = pd.Series([1.2 + 1j]) >>> s.abs() From 5e83e23d771539d8e5ae8005a9bb46bbf3517f3b Mon Sep 17 00:00:00 2001 From: Myles Braithwaite Date: Sun, 11 Mar 2018 16:49:55 -0400 Subject: [PATCH 7/7] :bulb: Add a timedelta example. --- pandas/core/generic.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3878597ddbc18..b9d97eec9a99c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7148,6 +7148,13 @@ def abs(self): 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 certian value using argsort (from `StackOverflow `__).