From 327345306e3c5b06b51ede1253fac1172ca5d2dc Mon Sep 17 00:00:00 2001 From: Alfonso MHC Date: Wed, 1 Apr 2015 13:19:30 +0200 Subject: [PATCH 1/3] Added documentation for mode() This relates to issue #9750 --- pandas/core/frame.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index fad271dbdb224..94fd951592009 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4411,9 +4411,13 @@ def _get_agg_axis(self, axis_num): def mode(self, axis=0, numeric_only=False): """ - Gets the mode of each element along the axis selected. Empty if nothing + Gets the mode(s) of each element along the axis selected. Empty if nothing has 2+ occurrences. Adds a row for each mode per label, fills in gaps - with nan. + with nan. Note that there could be multiple values returned for the selected + axis (when more than one item share the maximum frequency), which is the + reason why a dataframe is returned. This means that if you want to impute + missing values with the mode in a dataframe df, you can just do this: + df.fillna(df.mode().ix[0]) Parameters ---------- @@ -4426,6 +4430,14 @@ def mode(self, axis=0, numeric_only=False): Returns ------- modes : DataFrame (sorted) + + Examples + -------- + >>> df = pd.DataFrame({'A': [1, 2, 1, 2, 1, 2, 3]}) + >>> df.mode() + A + 0 1 + 1 2 """ data = self if not numeric_only else self._get_numeric_data() f = lambda s: s.mode() From e9cfaf662cc07f89db2714d9f15df1d84c982eee Mon Sep 17 00:00:00 2001 From: Alfonso MHC Date: Thu, 2 Apr 2015 15:12:00 +0200 Subject: [PATCH 2/3] Updated documentation for mode() Improved after review. Backtick for code mode, new paragraph for the note, iloc instead of ix for integer indices. --- pandas/core/frame.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 94fd951592009..2600d562a93da 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4413,11 +4413,12 @@ def mode(self, axis=0, numeric_only=False): """ Gets the mode(s) of each element along the axis selected. Empty if nothing has 2+ occurrences. Adds a row for each mode per label, fills in gaps - with nan. Note that there could be multiple values returned for the selected + with nan. + Note that there could be multiple values returned for the selected axis (when more than one item share the maximum frequency), which is the - reason why a dataframe is returned. This means that if you want to impute - missing values with the mode in a dataframe df, you can just do this: - df.fillna(df.mode().ix[0]) + reason why a dataframe is returned. If you want to impute missing values + with the mode in a dataframe ``df``, you can just do this: + ``df.fillna(df.mode().iloc[0])`` Parameters ---------- From 761be0fec7d010fc807aa28e62da61564c9847bd Mon Sep 17 00:00:00 2001 From: Alfonso MHC Date: Thu, 2 Apr 2015 20:20:18 +0200 Subject: [PATCH 3/3] Updated documentation for mode() added new line to make the note in a new paragraph --- pandas/core/frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 2600d562a93da..f700d4316842c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4414,6 +4414,7 @@ def mode(self, axis=0, numeric_only=False): Gets the mode(s) of each element along the axis selected. Empty if nothing has 2+ occurrences. Adds a row for each mode per label, fills in gaps with nan. + Note that there could be multiple values returned for the selected axis (when more than one item share the maximum frequency), which is the reason why a dataframe is returned. If you want to impute missing values