Skip to content

Commit a0558e7

Browse files
committed
use apply()-kwargs instead of partial, more tests, better examples
1 parent c8d3ac4 commit a0558e7

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

pandas/core/frame.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -4989,7 +4989,7 @@ def nunique(self, axis=0, dropna=True):
49894989
49904990
Examples
49914991
--------
4992-
>>> df = DataFrame({'A': [1, 2, 3], 'B': [1, 1, 1]})
4992+
>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, 1, 1]})
49934993
>>> df.nunique()
49944994
A 3
49954995
B 1
@@ -4999,8 +4999,7 @@ def nunique(self, axis=0, dropna=True):
49994999
1 2
50005000
2 2
50015001
"""
5002-
func = functools.partial(Series.nunique, dropna=dropna)
5003-
return self.apply(func, axis=axis)
5002+
return self.apply(Series.nunique, axis=axis, dropna=dropna)
50045003

50055004
def idxmin(self, axis=0, skipna=True):
50065005
"""

pandas/core/groupby.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -3917,9 +3917,10 @@ def nunique(self, dropna=True):
39173917
39183918
Examples
39193919
--------
3920-
>>> df = DataFrame({'id': ['spam', 'egg', 'egg', 'spam', 'ham', 'ham'],
3921-
... 'value1': [1, 5, 5, 2, 5, 5],
3922-
... 'value2': list('abbaxy')})
3920+
>>> df = pd.DataFrame({'id': ['spam', 'egg', 'egg', 'spam',
3921+
... 'ham', 'ham'],
3922+
... 'value1': [1, 5, 5, 2, 5, 5],
3923+
... 'value2': list('abbaxy')})
39233924
>>> df
39243925
id value1 value2
39253926
0 spam 1 a
@@ -3936,16 +3937,15 @@ def nunique(self, dropna=True):
39363937
ham 1 1 2
39373938
spam 1 2 1
39383939
3940+
# check for rows with the same id but conflicting values
39393941
>>> df.groupby('id').filter(lambda g: (g.nunique() > 1).any())
39403942
id value1 value2
39413943
0 spam 1 a
39423944
3 spam 2 a
39433945
4 ham 5 x
39443946
5 ham 5 y
39453947
"""
3946-
from functools import partial
3947-
func = partial(Series.nunique, dropna=dropna)
3948-
return self.apply(lambda g: g.apply(func))
3948+
return self.apply(lambda g: g.apply(Series.nunique, dropna=dropna))
39493949

39503950

39513951
from pandas.tools.plotting import boxplot_frame_groupby # noqa

pandas/tests/frame/test_analytics.py

+10
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@ def test_nunique(self):
416416
self._check_stat_op('nunique', f, has_skipna=False,
417417
check_dtype=False, check_dates=True)
418418

419+
df = DataFrame({'A': [1, 1, 1],
420+
'B': [1, 2, 3],
421+
'C': [1, np.nan, 3]})
422+
tm.assert_series_equal(df.nunique(), Series({'A': 1, 'B': 3, 'C': 2}))
423+
tm.assert_series_equal(df.nunique(dropna=False),
424+
Series({'A': 1, 'B': 3, 'C': 3}))
425+
tm.assert_series_equal(df.nunique(axis=1), Series({0: 1, 1: 2, 2: 2}))
426+
tm.assert_series_equal(df.nunique(axis=1, dropna=False),
427+
Series({0: 1, 1: 3, 2: 2}))
428+
419429
def test_sum(self):
420430
self._check_stat_op('sum', np.sum, has_numeric_only=True)
421431

0 commit comments

Comments
 (0)