Skip to content

Commit 79f41cc

Browse files
committed
implement additional tests for groupby apply methods
1 parent 4fed1e0 commit 79f41cc

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

pandas/tests/test_groupby.py

+54-1
Original file line numberDiff line numberDiff line change
@@ -1633,8 +1633,12 @@ def _testit(op):
16331633
result = op(grouped)['C']
16341634
assert_series_equal(result, exp)
16351635

1636+
_testit(lambda x: x.count())
16361637
_testit(lambda x: x.sum())
1638+
_testit(lambda x: x.std())
1639+
_testit(lambda x: x.var())
16371640
_testit(lambda x: x.mean())
1641+
_testit(lambda x: x.median())
16381642
_testit(lambda x: x.prod())
16391643
_testit(lambda x: x.min())
16401644
_testit(lambda x: x.max())
@@ -4166,7 +4170,7 @@ def test_tab_completion(self):
41664170
'agg','aggregate','apply','boxplot','filter','first','get_group',
41674171
'groups','hist','indices','last','max','mean','median',
41684172
'min','name','ngroups','nth','ohlc','plot', 'prod',
4169-
'size','std','sum','transform','var', 'count', 'head', 'describe',
4173+
'size', 'std', 'sum', 'transform', 'var', 'count', 'head', 'describe',
41704174
'cummax', 'quantile', 'rank', 'cumprod', 'tail',
41714175
'resample', 'cummin', 'fillna', 'cumsum', 'cumcount',
41724176
'all', 'shift', 'skew', 'bfill', 'irow', 'ffill',
@@ -4306,6 +4310,55 @@ def __eq__(self, other):
43064310
name='grp'))
43074311
tm.assert_frame_equal(result, expected)
43084312

4313+
def test__cython_agg_general(self):
4314+
ops = [('mean', np.mean),
4315+
('median', np.median),
4316+
('var', np.var),
4317+
('add', np.sum),
4318+
('prod', np.prod),
4319+
('min', np.min),
4320+
('max', np.max),
4321+
('first', lambda x: x.iloc[0]),
4322+
('last', lambda x: x.iloc[-1]),
4323+
('count', np.size),
4324+
]
4325+
df = DataFrame(np.random.randn(1000))
4326+
labels = np.random.randint(0, 50, size=1000).astype(float)
4327+
4328+
for op, targop in ops:
4329+
result = df.groupby(labels)._cython_agg_general(op)
4330+
expected = df.groupby(labels).agg(targop)
4331+
try:
4332+
tm.assert_frame_equal(result, expected)
4333+
except BaseException as exc:
4334+
exc.args += ('operation: %s' % op,)
4335+
raise
4336+
4337+
def test_ops_general(self):
4338+
ops = [('mean', np.mean),
4339+
('median', np.median),
4340+
('std', np.std),
4341+
('var', np.var),
4342+
('sum', np.sum),
4343+
('prod', np.prod),
4344+
('min', np.min),
4345+
('max', np.max),
4346+
('first', lambda x: x.iloc[0]),
4347+
('last', lambda x: x.iloc[-1]),
4348+
('count', np.size),
4349+
]
4350+
df = DataFrame(np.random.randn(1000))
4351+
labels = np.random.randint(0, 50, size=1000).astype(float)
4352+
4353+
for op, targop in ops:
4354+
result = getattr(df.groupby(labels), op)().astype(float)
4355+
expected = df.groupby(labels).agg(targop)
4356+
try:
4357+
tm.assert_frame_equal(result, expected)
4358+
except BaseException as exc:
4359+
exc.args += ('operation: %s' % op,)
4360+
raise
4361+
43094362

43104363
def assert_fp_equal(a, b):
43114364
assert (np.abs(a - b) < 1e-12).all()

0 commit comments

Comments
 (0)