Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add mode(dropna=True) #17547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions pandas/core/algorithms.py
Expand Up @@ -634,14 +634,16 @@ def duplicated(values, keep='first'):
return f(values, keep=keep)


def mode(values):
def mode(values, dropna=True):
"""
Returns the mode(s) of an array.

Parameters
----------
values : array-like
Array over which to check for duplicate values.
Array over which to check for duplicate values
dropna : boolean, default True
Don't include NaN values.

Returns
-------
Expand All @@ -666,6 +668,7 @@ def mode(values):
ndtype = 'object'
values = _ensure_object(values)

### TODO: IN HERE IMPLEMENT THE DROPNA PARAMETER
f = getattr(htable, "mode_{dtype}".format(dtype=ndtype))
result = f(values)
try:
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/frame.py
Expand Up @@ -5840,7 +5840,7 @@ def _get_agg_axis(self, axis_num):
else:
raise ValueError('Axis must be 0 or 1 (got %r)' % axis_num)

def mode(self, axis=0, numeric_only=False):
def mode(self, axis=0, numeric_only=False, dropna=True):
"""
Gets the mode(s) of each element along the axis selected. Adds a row
for each mode per label, fills in gaps with nan.
Expand All @@ -5858,6 +5858,8 @@ def mode(self, axis=0, numeric_only=False):
* 1 or 'columns' : get mode of each row
numeric_only : boolean, default False
if True, only apply to numeric columns
dropna : boolean, default True
Don't include NaN values.

Returns
-------
Expand All @@ -5874,7 +5876,7 @@ def mode(self, axis=0, numeric_only=False):
data = self if not numeric_only else self._get_numeric_data()

def f(s):
return s.mode()
return s.mode(dropna=dropna)

return data.apply(f, axis=axis)

Expand Down
7 changes: 6 additions & 1 deletion pandas/core/series.py
Expand Up @@ -1266,11 +1266,16 @@ def count(self, level=None):
return self._constructor(out, index=lev,
dtype='int64').__finalize__(self)

def mode(self):
def mode(self, dropna=True):
"""Return the mode(s) of the dataset.

Always returns Series even if only one value is returned.

Parameters
----------
dropna : boolean, default True
Don't include NaN values.

Returns
-------
modes : Series (sorted)
Expand Down