From aed542034b9571207296fff0cc7192dfc21e9d46 Mon Sep 17 00:00:00 2001 From: Asier Zugarramurdi Date: Sat, 10 Mar 2018 11:54:55 +0100 Subject: [PATCH 1/8] Fix docstring DataFrame.applymap --- pandas/core/frame.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index a66d00fff9714..5fdfe4aae4f0d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5017,18 +5017,18 @@ def applymap(self, func): Examples -------- - >>> df = pd.DataFrame(np.random.randn(3, 3)) + >>> df = pd.DataFrame(np.random.RandomState(0).randn(3, 3)) >>> df - 0 1 2 - 0 -0.029638 1.081563 1.280300 - 1 0.647747 0.831136 -1.549481 - 2 0.513416 -0.884417 0.195343 + 0 1 2 + 0 1.764052 0.400157 0.978738 + 1 2.240893 1.867558 -0.977278 + 2 0.950088 -0.151357 -0.103219 >>> df = df.applymap(lambda x: '%.2f' % x) >>> df - 0 1 2 - 0 -0.03 1.08 1.28 - 1 0.65 0.83 -1.55 - 2 0.51 -0.88 0.20 + 0 1 2 + 0 1.76 0.40 0.98 + 1 2.24 1.87 -0.98 + 2 0.95 -0.15 -0.10 Returns ------- @@ -5037,7 +5037,6 @@ def applymap(self, func): See also -------- DataFrame.apply : For operations on rows/columns - """ # if we have a dtype == 'M8[ns]', provide boxed values From 50bda40bf200b4652225dc3c5ea07350096d2681 Mon Sep 17 00:00:00 2001 From: Asier Zugarramurdi Date: Sat, 10 Mar 2018 14:10:07 +0100 Subject: [PATCH 2/8] Adapt docstring DataFrame.applymap to guidelines --- pandas/core/frame.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 5fdfe4aae4f0d..14fed72f4aea0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5005,14 +5005,25 @@ def apply(self, func, axis=0, broadcast=None, raw=False, reduce=None, def applymap(self, func): """ - Apply a function to a DataFrame that is intended to operate - elementwise, i.e. like doing map(func, series) for each series in the - DataFrame + Apply a function to a Dataframe elementwise. + + This method applies a function to a DataFrame that is intended to + operate elementwise, i.e. like doing map(func, series) for each series + in the DataFrame. Parameters ---------- func : function - Python function, returns a single value from a single value + Python function, returns a single value from a single value. + + Returns + ------- + pandas.DataFrame + A transformed DataFrame. + + See also + -------- + DataFrame.apply : Apply a function along input axis of DataFrame Examples -------- @@ -5029,14 +5040,6 @@ def applymap(self, func): 0 1.76 0.40 0.98 1 2.24 1.87 -0.98 2 0.95 -0.15 -0.10 - - Returns - ------- - applied : DataFrame - - See also - -------- - DataFrame.apply : For operations on rows/columns """ # if we have a dtype == 'M8[ns]', provide boxed values From 608cd1e4380c0a7f1633106caae5036f9223d94b Mon Sep 17 00:00:00 2001 From: Asier Zugarramurdi Date: Sat, 10 Mar 2018 14:21:18 +0100 Subject: [PATCH 3/8] Remove trailing space from docstring in DataFrame.applymap --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 14fed72f4aea0..03ede4948ea5d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5007,7 +5007,7 @@ def applymap(self, func): """ Apply a function to a Dataframe elementwise. - This method applies a function to a DataFrame that is intended to + This method applies a function to a DataFrame that is intended to operate elementwise, i.e. like doing map(func, series) for each series in the DataFrame. From 36690b0566a370af3680de60542d8d0c29e78299 Mon Sep 17 00:00:00 2001 From: Asier Zugarramurdi Date: Sat, 10 Mar 2018 16:21:18 +0100 Subject: [PATCH 4/8] Improve example in DataFrame.applymap docstring --- pandas/core/frame.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 03ede4948ea5d..2300d1c87d3bb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5027,19 +5027,16 @@ def applymap(self, func): Examples -------- - - >>> df = pd.DataFrame(np.random.RandomState(0).randn(3, 3)) + >>> df = pd.DataFrame([[1.,2.], [3.,4.]]) >>> df - 0 1 2 - 0 1.764052 0.400157 0.978738 - 1 2.240893 1.867558 -0.977278 - 2 0.950088 -0.151357 -0.103219 - >>> df = df.applymap(lambda x: '%.2f' % x) + 0 1 + 0 1.0 2.0 + 1 3.0 4.0 + >>> df = df.applymap(lambda x: x**2) >>> df - 0 1 2 - 0 1.76 0.40 0.98 - 1 2.24 1.87 -0.98 - 2 0.95 -0.15 -0.10 + 0 1 + 0 1.0 4.0 + 1 9.0 16.0 """ # if we have a dtype == 'M8[ns]', provide boxed values From 2ac22c9ccc51a74d4062df745b95d3bf3ae82513 Mon Sep 17 00:00:00 2001 From: Asier Zugarramurdi Date: Sat, 10 Mar 2018 16:44:02 +0100 Subject: [PATCH 5/8] add quote marks in DataFrame.applymap docstring --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2300d1c87d3bb..eb2e7ad1c0d0b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5008,7 +5008,7 @@ def applymap(self, func): Apply a function to a Dataframe elementwise. This method applies a function to a DataFrame that is intended to - operate elementwise, i.e. like doing map(func, series) for each series + operate elementwise, i.e. like doing `map(func, series)` for each series in the DataFrame. Parameters From 5c59ed44fcac98d030ec220aaae57154e13ae9e0 Mon Sep 17 00:00:00 2001 From: Asier Zugarramurdi Date: Sat, 10 Mar 2018 16:47:14 +0100 Subject: [PATCH 6/8] shorten line in DataFrame.applymap docstring --- pandas/core/frame.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index eb2e7ad1c0d0b..d1974b0e66240 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5008,8 +5008,8 @@ def applymap(self, func): Apply a function to a Dataframe elementwise. This method applies a function to a DataFrame that is intended to - operate elementwise, i.e. like doing `map(func, series)` for each series - in the DataFrame. + operate elementwise, i.e. like doing `map(func, series)` for each + series in the DataFrame. Parameters ---------- From e544bc0e8fefdd55cdea2e67119570ccd865695a Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 16 Mar 2018 16:31:46 -0500 Subject: [PATCH 7/8] Updated [ci skip] [ci skip] --- pandas/core/frame.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d1974b0e66240..f1e81ff19f205 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5007,19 +5007,18 @@ def applymap(self, func): """ Apply a function to a Dataframe elementwise. - This method applies a function to a DataFrame that is intended to - operate elementwise, i.e. like doing `map(func, series)` for each - series in the DataFrame. + This method applies a function that accepts and returns a scalar + to every element of a DataFrame. Parameters ---------- - func : function + func : callable Python function, returns a single value from a single value. Returns ------- - pandas.DataFrame - A transformed DataFrame. + DataFrame + Transformed DataFrame. See also -------- @@ -5027,16 +5026,31 @@ def applymap(self, func): Examples -------- - >>> df = pd.DataFrame([[1.,2.], [3.,4.]]) - >>> df - 0 1 - 0 1.0 2.0 - 1 3.0 4.0 - >>> df = df.applymap(lambda x: x**2) + >>> df = pd.DataFrame([[1, 2.12], [3.356, 4.567]]) >>> df - 0 1 - 0 1.0 4.0 - 1 9.0 16.0 + 0 1 + 0 1.000 2.120 + 1 3.356 4.567 + + >>> df.applymap(lambda x: len(str(x))) + 0 1 + 0 3 4 + 1 5 5 + + Note that a vectorized versions of `func` often exists, which will + be much faster. You could square each number elementwise. + + >>> df.applymap(lambda x: x**2) + 0 1 + 0 1.000000 4.494400 + 1 11.262736 20.857489 + + But it's better to avoid applymap in that case. + + >>> df ** 2 + 0 1 + 0 1.000000 4.494400 + 1 11.262736 20.857489 """ # if we have a dtype == 'M8[ns]', provide boxed values From 26c5aa821957195e63b1216b6bf2c8b0ee28cdf5 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 16 Mar 2018 16:32:36 -0500 Subject: [PATCH 8/8] Updated [ci skip] [ci skip] --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f1e81ff19f205..16575815a91d2 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5037,7 +5037,7 @@ def applymap(self, func): 0 3 4 1 5 5 - Note that a vectorized versions of `func` often exists, which will + Note that a vectorized version of `func` often exists, which will be much faster. You could square each number elementwise. >>> df.applymap(lambda x: x**2)