Skip to content

Commit 327f1a9

Browse files
author
tp
committed
new tests
1 parent f91b716 commit 327f1a9

File tree

2 files changed

+119
-40
lines changed

2 files changed

+119
-40
lines changed

pandas/tests/frame/test_apply.py

+58-19
Original file line numberDiff line numberDiff line change
@@ -1057,26 +1057,65 @@ def test_non_callable_aggregates(self):
10571057

10581058
assert result == expected
10591059

1060-
@pytest.mark.parametrize("df", [
1061-
pd.DataFrame([[1, 2], [3, 4]]),
1062-
pd.DataFrame([[np.nan, 2], [3, 4]]),
1063-
pd.DataFrame(),
1060+
@pytest.mark.parametrize("inputs", [
1061+
[DataFrame(), {
1062+
'sum': Series(),
1063+
'max': Series(),
1064+
'min': Series(),
1065+
'all': Series(dtype=bool),
1066+
'any': Series(dtype=bool),
1067+
'mean': Series(),
1068+
'prod': Series(),
1069+
'std': Series(),
1070+
'var': Series(),
1071+
'median': Series(),
1072+
'cumprod': DataFrame(),
1073+
'cumsum': DataFrame(),
1074+
}],
1075+
[DataFrame([[np.nan, 1], [1, 2]]), {
1076+
'sum': Series([1., 3]),
1077+
'max': Series([1., 2]),
1078+
'min': Series([1., 1]),
1079+
'all': Series([True, True]),
1080+
'any': Series([True, True]),
1081+
'mean': Series([1, 1.5]),
1082+
'prod': Series([1., 2]),
1083+
'std': Series([np.nan, 0.707107]),
1084+
'var': Series([np.nan, 0.5]),
1085+
'median': Series([1, 1.5]),
1086+
'cumprod': DataFrame([[np.nan, 1], [1., 2.]]),
1087+
'cumsum': DataFrame([[np.nan, 1], [1., 3.]]),
1088+
}],
1089+
[DataFrame([['a', 'b'], ['b', 'a']]), {
1090+
'sum': Series(['ab', 'ba']),
1091+
'max': Series(['b', 'b']),
1092+
'min': Series(['a', 'a']),
1093+
'all': Series([True, True]),
1094+
'any': Series([True, True]),
1095+
'mean': Series([], index=pd.Index([], dtype='int64')),
1096+
'prod': Series([], index=pd.Index([], dtype='int64')),
1097+
'std': Series([], index=pd.Index([], dtype='int64')),
1098+
'var': Series([], index=pd.Index([], dtype='int64')),
1099+
'median': Series([], index=pd.Index([], dtype='int64')),
1100+
'cumprod': TypeError,
1101+
'cumsum': DataFrame([['a', 'b'], ['ab', 'ba']]),
1102+
}],
10641103
])
1065-
def test_agg_function_input(self, df, cython_table_items):
1066-
# test whether the functions (keys) in
1067-
# pd.core.base.SelectionMixin._cython_table give the same result
1068-
# as the related strings (values) when used in df.agg. Examples:
1069-
# - ``df.agg(np.nansum)`` should give the same result as
1070-
# ``df.agg('sum')``
1071-
# - ``df.agg(sum)`` should give the same result as ``df.agg('sum')``
1072-
# etc.
1104+
@pytest.mark.parametrize("axis", [0, 1], ids=lambda x: "axis {}".format(x))
1105+
def test_agg_function_input(self, cython_table_items, inputs, axis):
10731106
# GH21123
10741107
np_func, str_func = cython_table_items
1075-
if str_func in ('cumprod', 'cumsum'):
1076-
tm.assert_frame_equal(df.agg(np_func), df.agg(str_func))
1077-
tm.assert_frame_equal(df.agg(np_func, axis=1),
1078-
df.agg(str_func, axis=1))
1108+
df = inputs[0]
1109+
expected = inputs[1][str_func]
1110+
1111+
if isinstance(expected, type) and issubclass(expected, Exception):
1112+
with pytest.raises(expected):
1113+
# e.g. DataFrame(['a b'.split()]).cumprod() will raise
1114+
df.agg(np_func, axis=axis)
1115+
df.agg(str_func, axis=axis)
1116+
elif str_func in ('cumprod', 'cumsum'):
1117+
tm.assert_frame_equal(df.agg(np_func, axis=axis), expected)
1118+
tm.assert_frame_equal(df.agg(str_func, axis=axis), expected)
10791119
else:
1080-
tm.assert_series_equal(df.agg(np_func), df.agg(str_func))
1081-
tm.assert_series_equal(df.agg(np_func, axis=1),
1082-
df.agg(str_func, axis=1))
1120+
tm.assert_series_equal(df.agg(np_func, axis=axis), expected)
1121+
tm.assert_series_equal(df.agg(str_func, axis=axis), expected)

pandas/tests/series/test_apply.py

+61-21
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,67 @@ def test_non_callable_aggregates(self):
331331
('mean', 1.5)]))
332332
assert_series_equal(result[expected.index], expected)
333333

334+
@pytest.mark.parametrize("inputs", [
335+
[Series(), {
336+
'sum': 0,
337+
'max': np.nan,
338+
'min': np.nan,
339+
'all': True,
340+
'any': False,
341+
'mean': np.nan,
342+
'prod': 1,
343+
'std': np.nan,
344+
'var': np.nan,
345+
'median': np.nan,
346+
'cumprod': Series([], Index([])),
347+
'cumsum': Series([], Index([])),
348+
}],
349+
[Series([np.nan, 1, 2, 3]), {
350+
'sum': 6,
351+
'max': 3,
352+
'min': 1,
353+
'all': True,
354+
'any': True,
355+
'mean': 2,
356+
'prod': 6,
357+
'std': 1,
358+
'var': 1,
359+
'median': 2,
360+
'cumprod': Series([np.nan, 1, 2, 6]),
361+
'cumsum': Series([np.nan, 1, 3, 6]),
362+
}],
363+
[Series('a b c'.split()), {
364+
'sum': 'abc',
365+
'max': 'b',
366+
'min': 'a',
367+
'all': 'c', # see GH12863
368+
'any': 'a',
369+
'mean': TypeError, # mean raises TypeError
370+
'prod': TypeError,
371+
'std': TypeError,
372+
'var': TypeError,
373+
'median': TypeError,
374+
'cumprod': TypeError,
375+
'cumsum': Series(['a', 'ab']),
376+
}],
377+
])
378+
def test_agg_function_input2(self, inputs, cython_table_items):
379+
# GH21123
380+
np_func, str_func = cython_table_items
381+
series = inputs[0]
382+
expected = inputs[1][str_func]
383+
384+
if isinstance(expected, type) and issubclass(expected, Exception):
385+
with pytest.raises(expected):
386+
series.agg(np_func)
387+
series.agg(str_func)
388+
elif str_func in ('cumprod', 'cumsum'):
389+
tm.assert_series_equal(series.agg(np_func), expected)
390+
tm.assert_series_equal(series.agg(str_func), expected)
391+
else:
392+
tm.assert_almost_equal(series.agg(np_func), expected)
393+
tm.assert_almost_equal(series.agg(str_func), expected)
394+
334395

335396
class TestSeriesMap(TestData):
336397

@@ -587,24 +648,3 @@ def test_map_missing_mixed(self, vals, mapping, exp):
587648
result = s.map(mapping)
588649

589650
tm.assert_series_equal(result, pd.Series(exp))
590-
591-
@pytest.mark.parametrize("series", [
592-
pd.Series([1, 2, 3, 4]),
593-
pd.Series([np.nan, 2, 3, 4]),
594-
pd.Series(),
595-
])
596-
def test_agg_function_input(self, series, cython_table_items):
597-
# test whether the functions (keys) in
598-
# pd.core.base.SelectionMixin._cython_table give the same result
599-
# as the related strings (values), when used in ser.agg. Examples:
600-
# - ``ser.agg(np.nansum)`` should give the same result as
601-
# ``ser.agg('sum')``
602-
# - ``ser.agg(sum)`` should give the same result as ``ser.agg('sum')``
603-
# etc.
604-
# GH21123
605-
np_func, str_func = cython_table_items
606-
607-
if str_func in ('cumprod', 'cumsum'):
608-
tm.assert_series_equal(series.agg(np_func), series.agg(str_func))
609-
else:
610-
tm.assert_almost_equal(series.agg(np_func), series.agg(str_func))

0 commit comments

Comments
 (0)