Skip to content

Commit 2313461

Browse files
author
tp
committed
add tests for func input to .agg to TestDataFrameAggregate and TestSeriesAggregate
1 parent 4543d07 commit 2313461

File tree

4 files changed

+71
-54
lines changed

4 files changed

+71
-54
lines changed

pandas/core/frame.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -5818,13 +5818,12 @@ def _gotitem(self,
58185818
def aggregate(self, func, axis=0, *args, **kwargs):
58195819
axis = self._get_axis_number(axis)
58205820

5821-
# TODO: flipped axis
58225821
result = None
5823-
if axis == 0:
5824-
try:
5825-
result, how = self._aggregate(func, axis=0, *args, **kwargs)
5826-
except TypeError:
5827-
pass
5822+
df = self if not axis else self.T
5823+
try:
5824+
result, how = df._aggregate(func, axis=0, *args, **kwargs)
5825+
except TypeError:
5826+
pass
58285827
if result is None:
58295828
return self.apply(func, axis=axis, args=args, **kwargs)
58305829
return result

pandas/tests/frame/test_apply.py

+34
Original file line numberDiff line numberDiff line change
@@ -1056,3 +1056,37 @@ def test_non_callable_aggregates(self):
10561056
expected = df.size
10571057

10581058
assert result == expected
1059+
1060+
@pytest.mark.parametrize("test_input", [
1061+
# is either frame or tuple (frame, args, kwargs),
1062+
pd.DataFrame([[1, 2], [3, 4]]),
1063+
pd.DataFrame([[np.nan, 2], [3, 4]]),
1064+
pd.DataFrame(),
1065+
])
1066+
@pytest.mark.parametrize(
1067+
"cython_table_items",
1068+
list(pd.core.base.SelectionMixin._cython_table.items()),
1069+
ids=lambda x: "({}-{!r})".format(x[0].__name__, x[1]),
1070+
)
1071+
def test_agg_function_input(self, cython_table_items, test_input):
1072+
# test whether the functions (keys) in
1073+
# pd.core.base.SelectionMixin._cython_table give the same result
1074+
# as the related strings (values) when used in df.agg. Examples:
1075+
# - ``df.agg(np.nansum)`` should give the same result as
1076+
# ``df.agg('sum')``
1077+
# - ``df.agg(sum)`` should give the same result as ``df.agg('sum')``
1078+
# etc.
1079+
# GH21123
1080+
np_func, str_func = cython_table_items
1081+
1082+
if isinstance(test_input, pd.DataFrame):
1083+
df, args, kwargs = test_input, (), {}
1084+
else:
1085+
df, args, kwargs = test_input
1086+
1087+
tm.assert_almost_equal(df.agg(np_func, *args, **kwargs),
1088+
df.agg(str_func, *args, **kwargs),
1089+
check_exact=True)
1090+
tm.assert_almost_equal(df.agg(np_func, axis=1, *args, **kwargs),
1091+
df.agg(str_func, axis=1, *args, **kwargs),
1092+
check_exact=True)

pandas/tests/series/test_apply.py

+31
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,34 @@ def test_map_missing_mixed(self, vals, mapping, exp):
587587
result = s.map(mapping)
588588

589589
tm.assert_series_equal(result, pd.Series(exp))
590+
591+
@pytest.mark.parametrize("test_input", [
592+
# is either series or tuple (series, args, kwargs),
593+
pd.Series([1, 2, 3, 4]),
594+
pd.Series([np.nan, 2, 3, 4]),
595+
pd.Series(),
596+
])
597+
@pytest.mark.parametrize(
598+
"cython_table_items",
599+
list(pd.core.base.SelectionMixin._cython_table.items()),
600+
ids=lambda x: "({}-{!r})".format(x[0].__name__, x[1]),
601+
)
602+
def test_agg_function_input(self, cython_table_items, test_input):
603+
# test whether the functions (keys) in
604+
# pd.core.base.SelectionMixin._cython_table give the same result
605+
# as the related strings (values), when used in ser.agg. Examples:
606+
# - ``ser.agg(np.nansum)`` should give the same result as
607+
# ``ser.agg('sum')``
608+
# - ``ser.agg(sum)`` should give the same result as ``ser.agg('sum')``
609+
# etc.
610+
# GH21123
611+
np_func, str_func = cython_table_items
612+
613+
if isinstance(test_input, (pd.DataFrame, pd.Series)):
614+
ser, args, kwargs = test_input, (), {}
615+
else:
616+
ser, args, kwargs = test_input
617+
618+
tm.assert_almost_equal(ser.agg(np_func, *args, **kwargs),
619+
ser.agg(str_func, *args, **kwargs),
620+
check_exact=True)

pandas/tests/test_nanops.py

+1-48
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import pandas.core.nanops as nanops
1414
import pandas.util.testing as tm
1515
import pandas.util._test_decorators as td
16-
from pandas.compat.numpy import (_np_version_under1p13, _np_version_under1p10,
17-
_np_version_under1p12)
16+
from pandas.compat.numpy import _np_version_under1p13
1817

1918
use_bn = nanops._USE_BOTTLENECK
2019

@@ -995,52 +994,6 @@ def prng(self):
995994
return np.random.RandomState(1234)
996995

997996

998-
@pytest.fixture(params=[
999-
pd.Series([1, 2, 3, 4]),
1000-
pd.DataFrame([[1, 2], [3, 4]]),
1001-
pd.Series([np.nan, 2, 3, 4]),
1002-
pd.DataFrame([[np.nan, 2], [3, 4]]),
1003-
pd.Series(),
1004-
pd.DataFrame(),
1005-
pd.Series([np.nan]),
1006-
pd.DataFrame([[np.nan]]),
1007-
])
1008-
def series_or_frame(request):
1009-
return request.param
1010-
1011-
1012-
@pytest.mark.parametrize("standard, nan_method", [
1013-
(np.sum, np.nansum),
1014-
(np.mean, np.nanmean),
1015-
(np.std, np.nanstd),
1016-
(np.var, np.nanvar),
1017-
(np.median, np.nanmedian),
1018-
(np.max, np.nanmax),
1019-
(np.min, np.nanmin),
1020-
], ids=lambda x: x.__name__)
1021-
def test_np_nan_functions(standard, nan_method, series_or_frame):
1022-
tm.assert_almost_equal(series_or_frame.agg(standard),
1023-
series_or_frame.agg(nan_method),
1024-
check_exact=True)
1025-
1026-
1027-
@pytest.mark.skipif(_np_version_under1p10, reason="requires numpy>=1.10")
1028-
def test_np_nanprod(series_or_frame):
1029-
tm.assert_almost_equal(series_or_frame.agg(np.prod),
1030-
series_or_frame.agg(np.nanprod),
1031-
check_exact=True)
1032-
1033-
1034-
@pytest.mark.skipif(_np_version_under1p12, reason="requires numpy>=1.12")
1035-
def test_np_nancumprod(series_or_frame):
1036-
funcs = [(np.cumprod, np.nancumprod),
1037-
(np.cumsum, np.nancumsum)]
1038-
for standard, nan_method in funcs:
1039-
tm.assert_almost_equal(series_or_frame.agg(standard),
1040-
series_or_frame.agg(nan_method),
1041-
check_exact=True)
1042-
1043-
1044997
def test_use_bottleneck():
1045998

1046999
if nanops._BOTTLENECK_INSTALLED:

0 commit comments

Comments
 (0)