Skip to content

Commit 87972a5

Browse files
committed
Implement mode(dropna), for pandas-dev#17534
1 parent c65a0f5 commit 87972a5

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

pandas/core/algorithms.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -634,14 +634,16 @@ def duplicated(values, keep='first'):
634634
return f(values, keep=keep)
635635

636636

637-
def mode(values):
637+
def mode(values, dropna=True):
638638
"""
639639
Returns the mode(s) of an array.
640640
641641
Parameters
642642
----------
643643
values : array-like
644-
Array over which to check for duplicate values.
644+
Array over which to check for duplicate values
645+
dropna : boolean, default True
646+
Don't include NaN values.
645647
646648
Returns
647649
-------
@@ -666,6 +668,7 @@ def mode(values):
666668
ndtype = 'object'
667669
values = _ensure_object(values)
668670

671+
### TODO: IN HERE IMPLEMENT THE DROPNA PARAMETER
669672
f = getattr(htable, "mode_{dtype}".format(dtype=ndtype))
670673
result = f(values)
671674
try:

pandas/core/frame.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5840,7 +5840,7 @@ def _get_agg_axis(self, axis_num):
58405840
else:
58415841
raise ValueError('Axis must be 0 or 1 (got %r)' % axis_num)
58425842

5843-
def mode(self, axis=0, numeric_only=False):
5843+
def mode(self, axis=0, numeric_only=False, dropna=True):
58445844
"""
58455845
Gets the mode(s) of each element along the axis selected. Adds a row
58465846
for each mode per label, fills in gaps with nan.
@@ -5858,6 +5858,8 @@ def mode(self, axis=0, numeric_only=False):
58585858
* 1 or 'columns' : get mode of each row
58595859
numeric_only : boolean, default False
58605860
if True, only apply to numeric columns
5861+
dropna : boolean, default True
5862+
Don't include NaN values.
58615863
58625864
Returns
58635865
-------
@@ -5874,7 +5876,7 @@ def mode(self, axis=0, numeric_only=False):
58745876
data = self if not numeric_only else self._get_numeric_data()
58755877

58765878
def f(s):
5877-
return s.mode()
5879+
return s.mode(dropna=dropna)
58785880

58795881
return data.apply(f, axis=axis)
58805882

pandas/core/series.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1266,11 +1266,16 @@ def count(self, level=None):
12661266
return self._constructor(out, index=lev,
12671267
dtype='int64').__finalize__(self)
12681268

1269-
def mode(self):
1269+
def mode(self, dropna=True):
12701270
"""Return the mode(s) of the dataset.
12711271
12721272
Always returns Series even if only one value is returned.
12731273
1274+
Parameters
1275+
----------
1276+
dropna : boolean, default True
1277+
Don't include NaN values.
1278+
12741279
Returns
12751280
-------
12761281
modes : Series (sorted)

0 commit comments

Comments
 (0)