Skip to content

Commit f946912

Browse files
authored
CLN: Group tests for apply/agg/transform with a string argument (#42264)
1 parent f1e4fb0 commit f946912

File tree

5 files changed

+289
-282
lines changed

5 files changed

+289
-282
lines changed

pandas/tests/apply/test_frame_apply.py

-122
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
from itertools import chain
32
import warnings
43

54
import numpy as np
@@ -159,32 +158,6 @@ def test_apply_standard_nonunique():
159158
tm.assert_series_equal(result, expected)
160159

161160

162-
@pytest.mark.parametrize("func", ["sum", "mean", "min", "max", "std"])
163-
@pytest.mark.parametrize(
164-
"args,kwds",
165-
[
166-
pytest.param([], {}, id="no_args_or_kwds"),
167-
pytest.param([1], {}, id="axis_from_args"),
168-
pytest.param([], {"axis": 1}, id="axis_from_kwds"),
169-
pytest.param([], {"numeric_only": True}, id="optional_kwds"),
170-
pytest.param([1, None], {"numeric_only": True}, id="args_and_kwds"),
171-
],
172-
)
173-
@pytest.mark.parametrize("how", ["agg", "apply"])
174-
def test_apply_with_string_funcs(request, float_frame, func, args, kwds, how):
175-
if len(args) > 1 and how == "agg":
176-
request.node.add_marker(
177-
pytest.mark.xfail(
178-
raises=TypeError,
179-
reason="agg/apply signature mismatch - agg passes 2nd "
180-
"argument to func",
181-
)
182-
)
183-
result = getattr(float_frame, how)(func, *args, **kwds)
184-
expected = getattr(float_frame, func)(*args, **kwds)
185-
tm.assert_series_equal(result, expected)
186-
187-
188161
def test_apply_broadcast(float_frame, int_frame_const_col):
189162

190163
# scalars
@@ -1312,76 +1285,6 @@ def func(group_col):
13121285
tm.assert_frame_equal(result, expected)
13131286

13141287

1315-
@pytest.mark.parametrize(
1316-
"df, func, expected",
1317-
chain(
1318-
tm.get_cython_table_params(
1319-
DataFrame(),
1320-
[
1321-
("sum", Series(dtype="float64")),
1322-
("max", Series(dtype="float64")),
1323-
("min", Series(dtype="float64")),
1324-
("all", Series(dtype=bool)),
1325-
("any", Series(dtype=bool)),
1326-
("mean", Series(dtype="float64")),
1327-
("prod", Series(dtype="float64")),
1328-
("std", Series(dtype="float64")),
1329-
("var", Series(dtype="float64")),
1330-
("median", Series(dtype="float64")),
1331-
],
1332-
),
1333-
tm.get_cython_table_params(
1334-
DataFrame([[np.nan, 1], [1, 2]]),
1335-
[
1336-
("sum", Series([1.0, 3])),
1337-
("max", Series([1.0, 2])),
1338-
("min", Series([1.0, 1])),
1339-
("all", Series([True, True])),
1340-
("any", Series([True, True])),
1341-
("mean", Series([1, 1.5])),
1342-
("prod", Series([1.0, 2])),
1343-
("std", Series([np.nan, 0.707107])),
1344-
("var", Series([np.nan, 0.5])),
1345-
("median", Series([1, 1.5])),
1346-
],
1347-
),
1348-
),
1349-
)
1350-
def test_agg_cython_table(df, func, expected, axis):
1351-
# GH 21224
1352-
# test reducing functions in
1353-
# pandas.core.base.SelectionMixin._cython_table
1354-
result = df.agg(func, axis=axis)
1355-
tm.assert_series_equal(result, expected)
1356-
1357-
1358-
@pytest.mark.parametrize(
1359-
"df, func, expected",
1360-
chain(
1361-
tm.get_cython_table_params(
1362-
DataFrame(), [("cumprod", DataFrame()), ("cumsum", DataFrame())]
1363-
),
1364-
tm.get_cython_table_params(
1365-
DataFrame([[np.nan, 1], [1, 2]]),
1366-
[
1367-
("cumprod", DataFrame([[np.nan, 1], [1, 2]])),
1368-
("cumsum", DataFrame([[np.nan, 1], [1, 3]])),
1369-
],
1370-
),
1371-
),
1372-
)
1373-
def test_agg_cython_table_transform(df, func, expected, axis):
1374-
# GH 21224
1375-
# test transforming functions in
1376-
# pandas.core.base.SelectionMixin._cython_table (cumprod, cumsum)
1377-
if axis == "columns" or axis == 1:
1378-
# operating blockwise doesn't let us preserve dtypes
1379-
expected = expected.astype("float64")
1380-
1381-
result = df.agg(func, axis=axis)
1382-
tm.assert_frame_equal(result, expected)
1383-
1384-
13851288
@pytest.mark.parametrize("axis", [0, 1])
13861289
@pytest.mark.parametrize(
13871290
"args, kwargs",
@@ -1510,31 +1413,6 @@ def test_apply_raw_returns_string():
15101413
tm.assert_series_equal(result, expected)
15111414

15121415

1513-
@pytest.mark.parametrize(
1514-
"op", ["abs", "ceil", "cos", "cumsum", "exp", "log", "sqrt", "square"]
1515-
)
1516-
@pytest.mark.parametrize("how", ["transform", "apply"])
1517-
def test_apply_np_transformer(float_frame, op, how):
1518-
# GH 39116
1519-
result = getattr(float_frame, how)(op)
1520-
expected = getattr(np, op)(float_frame)
1521-
tm.assert_frame_equal(result, expected)
1522-
1523-
1524-
@pytest.mark.parametrize("op", ["mean", "median", "std", "var"])
1525-
@pytest.mark.parametrize("how", ["agg", "apply"])
1526-
def test_apply_np_reducer(float_frame, op, how):
1527-
# GH 39116
1528-
float_frame = DataFrame({"a": [1, 2], "b": [3, 4]})
1529-
result = getattr(float_frame, how)(op)
1530-
# pandas ddof defaults to 1, numpy to 0
1531-
kwargs = {"ddof": 1} if op in ("std", "var") else {}
1532-
expected = Series(
1533-
getattr(np, op)(float_frame, axis=0, **kwargs), index=float_frame.columns
1534-
)
1535-
tm.assert_series_equal(result, expected)
1536-
1537-
15381416
def test_aggregation_func_column_order():
15391417
# GH40420: the result of .agg should have an index that is sorted
15401418
# according to the arguments provided to agg.

pandas/tests/apply/test_frame_transform.py

-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import operator
2-
31
import numpy as np
42
import pytest
53

@@ -38,40 +36,6 @@ def test_transform_ufunc(axis, float_frame, frame_or_series):
3836
tm.assert_equal(result, expected)
3937

4038

41-
@pytest.mark.parametrize("op", frame_transform_kernels)
42-
def test_transform_groupby_kernel(axis, float_frame, op, using_array_manager, request):
43-
# GH 35964
44-
if using_array_manager and op == "pct_change" and axis in (1, "columns"):
45-
# TODO(ArrayManager) shift with axis=1
46-
request.node.add_marker(
47-
pytest.mark.xfail(
48-
reason="shift axis=1 not yet implemented for ArrayManager"
49-
)
50-
)
51-
52-
args = [0.0] if op == "fillna" else []
53-
if axis == 0 or axis == "index":
54-
ones = np.ones(float_frame.shape[0])
55-
else:
56-
ones = np.ones(float_frame.shape[1])
57-
expected = float_frame.groupby(ones, axis=axis).transform(op, *args)
58-
result = float_frame.transform(op, axis, *args)
59-
tm.assert_frame_equal(result, expected)
60-
61-
# same thing, but ensuring we have multiple blocks
62-
assert "E" not in float_frame.columns
63-
float_frame["E"] = float_frame["A"].copy()
64-
assert len(float_frame._mgr.arrays) > 1
65-
66-
if axis == 0 or axis == "index":
67-
ones = np.ones(float_frame.shape[0])
68-
else:
69-
ones = np.ones(float_frame.shape[1])
70-
expected2 = float_frame.groupby(ones, axis=axis).transform(op, *args)
71-
result2 = float_frame.transform(op, axis, *args)
72-
tm.assert_frame_equal(result2, expected2)
73-
74-
7539
@pytest.mark.parametrize(
7640
"ops, names",
7741
[
@@ -162,15 +126,6 @@ def func(x):
162126
tm.assert_equal(result, expected)
163127

164128

165-
@pytest.mark.parametrize("method", ["abs", "shift", "pct_change", "cumsum", "rank"])
166-
def test_transform_method_name(method):
167-
# GH 19760
168-
df = DataFrame({"A": [-1, 2]})
169-
result = df.transform(method)
170-
expected = operator.methodcaller(method)(df)
171-
tm.assert_frame_equal(result, expected)
172-
173-
174129
wont_fail = ["ffill", "bfill", "fillna", "pad", "backfill", "shift"]
175130
frame_kernels_raise = [x for x in frame_transform_kernels if x not in wont_fail]
176131

pandas/tests/apply/test_series_apply.py

-97
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
Counter,
33
defaultdict,
44
)
5-
from itertools import chain
65

76
import numpy as np
87
import pytest
98

10-
from pandas.core.dtypes.common import is_number
11-
129
import pandas as pd
1310
from pandas import (
1411
DataFrame,
@@ -87,14 +84,6 @@ def f(x):
8784
assert result.dtype == object
8885

8986

90-
def test_with_string_args(datetime_series):
91-
92-
for arg in ["sum", "mean", "min", "max", "std"]:
93-
result = datetime_series.apply(arg)
94-
expected = getattr(datetime_series, arg)()
95-
assert result == expected
96-
97-
9887
def test_apply_args():
9988
s = Series(["foo,bar"])
10089

@@ -418,92 +407,6 @@ def test_non_callable_aggregates(how):
418407
tm.assert_series_equal(result, expected)
419408

420409

421-
@pytest.mark.parametrize(
422-
"series, func, expected",
423-
chain(
424-
tm.get_cython_table_params(
425-
Series(dtype=np.float64),
426-
[
427-
("sum", 0),
428-
("max", np.nan),
429-
("min", np.nan),
430-
("all", True),
431-
("any", False),
432-
("mean", np.nan),
433-
("prod", 1),
434-
("std", np.nan),
435-
("var", np.nan),
436-
("median", np.nan),
437-
],
438-
),
439-
tm.get_cython_table_params(
440-
Series([np.nan, 1, 2, 3]),
441-
[
442-
("sum", 6),
443-
("max", 3),
444-
("min", 1),
445-
("all", True),
446-
("any", True),
447-
("mean", 2),
448-
("prod", 6),
449-
("std", 1),
450-
("var", 1),
451-
("median", 2),
452-
],
453-
),
454-
tm.get_cython_table_params(
455-
Series("a b c".split()),
456-
[
457-
("sum", "abc"),
458-
("max", "c"),
459-
("min", "a"),
460-
("all", True),
461-
("any", True),
462-
],
463-
),
464-
),
465-
)
466-
def test_agg_cython_table(series, func, expected):
467-
# GH21224
468-
# test reducing functions in
469-
# pandas.core.base.SelectionMixin._cython_table
470-
result = series.agg(func)
471-
if is_number(expected):
472-
assert np.isclose(result, expected, equal_nan=True)
473-
else:
474-
assert result == expected
475-
476-
477-
@pytest.mark.parametrize(
478-
"series, func, expected",
479-
chain(
480-
tm.get_cython_table_params(
481-
Series(dtype=np.float64),
482-
[
483-
("cumprod", Series([], Index([]), dtype=np.float64)),
484-
("cumsum", Series([], Index([]), dtype=np.float64)),
485-
],
486-
),
487-
tm.get_cython_table_params(
488-
Series([np.nan, 1, 2, 3]),
489-
[
490-
("cumprod", Series([np.nan, 1, 2, 6])),
491-
("cumsum", Series([np.nan, 1, 3, 6])),
492-
],
493-
),
494-
tm.get_cython_table_params(
495-
Series("a b c".split()), [("cumsum", Series(["a", "ab", "abc"]))]
496-
),
497-
),
498-
)
499-
def test_agg_cython_table_transform(series, func, expected):
500-
# GH21224
501-
# test transforming functions in
502-
# pandas.core.base.SelectionMixin._cython_table (cumprod, cumsum)
503-
result = series.agg(func)
504-
tm.assert_series_equal(result, expected)
505-
506-
507410
def test_series_apply_no_suffix_index():
508411
# GH36189
509412
s = Series([4] * 3)

pandas/tests/apply/test_series_transform.py

-18
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,6 @@
88
concat,
99
)
1010
import pandas._testing as tm
11-
from pandas.core.groupby.base import transformation_kernels
12-
13-
# tshift only works on time index and is deprecated
14-
# There is no Series.cumcount
15-
series_kernels = [
16-
x for x in sorted(transformation_kernels) if x not in ["tshift", "cumcount"]
17-
]
18-
19-
20-
@pytest.mark.parametrize("op", series_kernels)
21-
def test_transform_groupby_kernel(string_series, op):
22-
# GH 35964
23-
24-
args = [0.0] if op == "fillna" else []
25-
ones = np.ones(string_series.shape[0])
26-
expected = string_series.groupby(ones).transform(op, *args)
27-
result = string_series.transform(op, 0, *args)
28-
tm.assert_series_equal(result, expected)
2911

3012

3113
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)