Skip to content

Commit 2aa1ab4

Browse files
authored
TST/REF: Consolidate tests in apply.test_invalid_arg (#40092)
1 parent c5d0d13 commit 2aa1ab4

7 files changed

+302
-293
lines changed

pandas/tests/apply/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import DataFrame
5+
6+
7+
@pytest.fixture
8+
def int_frame_const_col():
9+
"""
10+
Fixture for DataFrame of ints which are constant per column
11+
12+
Columns are ['A', 'B', 'C'], with values (per column): [1, 2, 3]
13+
"""
14+
df = DataFrame(
15+
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
16+
columns=["A", "B", "C"],
17+
)
18+
return df

pandas/tests/apply/test_frame_apply.py

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,11 @@
1414
Series,
1515
Timestamp,
1616
date_range,
17-
notna,
1817
)
1918
import pandas._testing as tm
20-
from pandas.core.base import SpecificationError
2119
from pandas.tests.frame.common import zip_frames
2220

2321

24-
@pytest.fixture
25-
def int_frame_const_col():
26-
"""
27-
Fixture for DataFrame of ints which are constant per column
28-
29-
Columns are ['A', 'B', 'C'], with values (per column): [1, 2, 3]
30-
"""
31-
df = DataFrame(
32-
np.tile(np.arange(3, dtype="int64"), 6).reshape(6, -1) + 1,
33-
columns=["A", "B", "C"],
34-
)
35-
return df
36-
37-
3822
def test_apply(float_frame):
3923
with np.errstate(all="ignore"):
4024
# ufunc
@@ -195,17 +179,6 @@ def test_apply_with_string_funcs(request, float_frame, func, args, kwds, how):
195179
tm.assert_series_equal(result, expected)
196180

197181

198-
@pytest.mark.parametrize(
199-
"how, args", [("pct_change", ()), ("nsmallest", (1, ["a", "b"])), ("tail", 1)]
200-
)
201-
def test_apply_str_axis_1_raises(how, args):
202-
# GH 39211 - some ops don't support axis=1
203-
df = DataFrame({"a": [1, 2], "b": [3, 4]})
204-
msg = f"Operation {how} does not support axis=1"
205-
with pytest.raises(ValueError, match=msg):
206-
df.apply(how, axis=1, args=args)
207-
208-
209182
def test_apply_broadcast(float_frame, int_frame_const_col):
210183

211184
# scalars
@@ -259,27 +232,6 @@ def test_apply_broadcast(float_frame, int_frame_const_col):
259232
tm.assert_frame_equal(result, expected)
260233

261234

262-
def test_apply_broadcast_error(int_frame_const_col):
263-
df = int_frame_const_col
264-
265-
# > 1 ndim
266-
msg = "too many dims to broadcast"
267-
with pytest.raises(ValueError, match=msg):
268-
df.apply(
269-
lambda x: np.array([1, 2]).reshape(-1, 2),
270-
axis=1,
271-
result_type="broadcast",
272-
)
273-
274-
# cannot broadcast
275-
msg = "cannot broadcast result"
276-
with pytest.raises(ValueError, match=msg):
277-
df.apply(lambda x: [1, 2], axis=1, result_type="broadcast")
278-
279-
with pytest.raises(ValueError, match=msg):
280-
df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast")
281-
282-
283235
def test_apply_raw(float_frame, mixed_type_frame):
284236
def _assert_raw(x):
285237
assert isinstance(x, np.ndarray)
@@ -425,71 +377,6 @@ def test_apply_differently_indexed():
425377
tm.assert_frame_equal(result, expected)
426378

427379

428-
def test_apply_modify_traceback():
429-
data = DataFrame(
430-
{
431-
"A": [
432-
"foo",
433-
"foo",
434-
"foo",
435-
"foo",
436-
"bar",
437-
"bar",
438-
"bar",
439-
"bar",
440-
"foo",
441-
"foo",
442-
"foo",
443-
],
444-
"B": [
445-
"one",
446-
"one",
447-
"one",
448-
"two",
449-
"one",
450-
"one",
451-
"one",
452-
"two",
453-
"two",
454-
"two",
455-
"one",
456-
],
457-
"C": [
458-
"dull",
459-
"dull",
460-
"shiny",
461-
"dull",
462-
"dull",
463-
"shiny",
464-
"shiny",
465-
"dull",
466-
"shiny",
467-
"shiny",
468-
"shiny",
469-
],
470-
"D": np.random.randn(11),
471-
"E": np.random.randn(11),
472-
"F": np.random.randn(11),
473-
}
474-
)
475-
476-
data.loc[4, "C"] = np.nan
477-
478-
def transform(row):
479-
if row["C"].startswith("shin") and row["A"] == "foo":
480-
row["D"] = 7
481-
return row
482-
483-
def transform2(row):
484-
if notna(row["C"]) and row["C"].startswith("shin") and row["A"] == "foo":
485-
row["D"] = 7
486-
return row
487-
488-
msg = "'float' object has no attribute 'startswith'"
489-
with pytest.raises(AttributeError, match=msg):
490-
data.apply(transform, axis=1)
491-
492-
493380
def test_apply_bug():
494381

495382
# GH 6125
@@ -1105,19 +992,6 @@ def test_result_type(int_frame_const_col):
1105992
tm.assert_frame_equal(result, expected)
1106993

1107994

1108-
@pytest.mark.parametrize("result_type", ["foo", 1])
1109-
def test_result_type_error(result_type, int_frame_const_col):
1110-
# allowed result_type
1111-
df = int_frame_const_col
1112-
1113-
msg = (
1114-
"invalid value for result_type, must be one of "
1115-
"{None, 'reduce', 'broadcast', 'expand'}"
1116-
)
1117-
with pytest.raises(ValueError, match=msg):
1118-
df.apply(lambda x: [1, 2, 3], axis=1, result_type=result_type)
1119-
1120-
1121995
@pytest.mark.parametrize(
1122996
"box",
1123997
[lambda x: list(x), lambda x: tuple(x), lambda x: np.array(x, dtype="int64")],
@@ -1174,20 +1048,6 @@ def test_agg_transform(axis, float_frame):
11741048
tm.assert_frame_equal(result, expected)
11751049

11761050

1177-
def test_transform_and_agg_err(axis, float_frame):
1178-
# cannot both transform and agg
1179-
msg = "cannot combine transform and aggregation operations"
1180-
with pytest.raises(ValueError, match=msg):
1181-
with np.errstate(all="ignore"):
1182-
float_frame.agg(["max", "sqrt"], axis=axis)
1183-
1184-
df = DataFrame({"A": range(5), "B": 5})
1185-
1186-
def f():
1187-
with np.errstate(all="ignore"):
1188-
df.agg({"A": ["abs", "sum"], "B": ["mean", "max"]}, axis=axis)
1189-
1190-
11911051
def test_demo():
11921052
# demonstration tests
11931053
df = DataFrame({"A": range(5), "B": 5})
@@ -1258,16 +1118,6 @@ def test_agg_multiple_mixed_no_warning():
12581118
tm.assert_frame_equal(result, expected)
12591119

12601120

1261-
def test_agg_dict_nested_renaming_depr():
1262-
1263-
df = DataFrame({"A": range(5), "B": 5})
1264-
1265-
# nested renaming
1266-
msg = r"nested renamer is not supported"
1267-
with pytest.raises(SpecificationError, match=msg):
1268-
df.agg({"A": {"foo": "min"}, "B": {"bar": "max"}})
1269-
1270-
12711121
def test_agg_reduce(axis, float_frame):
12721122
other_axis = 1 if axis in {0, "index"} else 0
12731123
name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values()
@@ -1520,19 +1370,6 @@ def test_agg_cython_table_transform(df, func, expected, axis):
15201370
tm.assert_frame_equal(result, expected)
15211371

15221372

1523-
@pytest.mark.parametrize(
1524-
"df, func, expected",
1525-
tm.get_cython_table_params(
1526-
DataFrame([["a", "b"], ["b", "a"]]), [["cumprod", TypeError]]
1527-
),
1528-
)
1529-
def test_agg_cython_table_raises(df, func, expected, axis):
1530-
# GH 21224
1531-
msg = "can't multiply sequence by non-int of type 'str'"
1532-
with pytest.raises(expected, match=msg):
1533-
df.agg(func, axis=axis)
1534-
1535-
15361373
@pytest.mark.parametrize("axis", [0, 1])
15371374
@pytest.mark.parametrize(
15381375
"args, kwargs",

pandas/tests/apply/test_frame_apply_relabeling.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import pytest
32

43
import pandas as pd
54
import pandas._testing as tm
@@ -96,12 +95,3 @@ def test_agg_namedtuple():
9695
index=pd.Index(["foo", "bar", "cat"]),
9796
)
9897
tm.assert_frame_equal(result, expected)
99-
100-
101-
def test_agg_raises():
102-
# GH 26513
103-
df = pd.DataFrame({"A": [0, 1], "B": [1, 2]})
104-
msg = "Must provide"
105-
106-
with pytest.raises(TypeError, match=msg):
107-
df.agg()

pandas/tests/apply/test_frame_transform.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import operator
2-
import re
32

43
import numpy as np
54
import pytest
@@ -10,7 +9,6 @@
109
Series,
1110
)
1211
import pandas._testing as tm
13-
from pandas.core.base import SpecificationError
1412
from pandas.core.groupby.base import transformation_kernels
1513
from pandas.tests.frame.common import zip_frames
1614

@@ -159,47 +157,6 @@ def test_transform_method_name(method):
159157
tm.assert_frame_equal(result, expected)
160158

161159

162-
def test_transform_and_agg_err(axis, float_frame):
163-
# GH 35964
164-
# cannot both transform and agg
165-
msg = "Function did not transform"
166-
with pytest.raises(ValueError, match=msg):
167-
float_frame.transform(["max", "min"], axis=axis)
168-
169-
msg = "Function did not transform"
170-
with pytest.raises(ValueError, match=msg):
171-
float_frame.transform(["max", "sqrt"], axis=axis)
172-
173-
174-
def test_agg_dict_nested_renaming_depr():
175-
df = DataFrame({"A": range(5), "B": 5})
176-
177-
# nested renaming
178-
msg = r"nested renamer is not supported"
179-
with pytest.raises(SpecificationError, match=msg):
180-
# mypy identifies the argument as an invalid type
181-
df.transform({"A": {"foo": "min"}, "B": {"bar": "max"}})
182-
183-
184-
def test_transform_reducer_raises(all_reductions, frame_or_series):
185-
# GH 35964
186-
op = all_reductions
187-
188-
obj = DataFrame({"A": [1, 2, 3]})
189-
if frame_or_series is not DataFrame:
190-
obj = obj["A"]
191-
192-
msg = "Function did not transform"
193-
with pytest.raises(ValueError, match=msg):
194-
obj.transform(op)
195-
with pytest.raises(ValueError, match=msg):
196-
obj.transform([op])
197-
with pytest.raises(ValueError, match=msg):
198-
obj.transform({"A": op})
199-
with pytest.raises(ValueError, match=msg):
200-
obj.transform({"A": [op]})
201-
202-
203160
wont_fail = ["ffill", "bfill", "fillna", "pad", "backfill", "shift"]
204161
frame_kernels_raise = [x for x in frame_kernels if x not in wont_fail]
205162

@@ -267,30 +224,6 @@ def f(x, a, b, c):
267224
frame_or_series([1]).transform(f, 0, *expected_args, **expected_kwargs)
268225

269226

270-
def test_transform_missing_columns(axis):
271-
# GH#35964
272-
df = DataFrame({"A": [1, 2], "B": [3, 4]})
273-
match = re.escape("Column(s) ['C'] do not exist")
274-
with pytest.raises(KeyError, match=match):
275-
df.transform({"C": "cumsum"})
276-
277-
278-
def test_transform_none_to_type():
279-
# GH#34377
280-
df = DataFrame({"a": [None]})
281-
msg = "Transform function failed"
282-
with pytest.raises(ValueError, match=msg):
283-
df.transform({"a": int})
284-
285-
286-
def test_transform_mixed_column_name_dtypes():
287-
# GH39025
288-
df = DataFrame({"a": ["1"]})
289-
msg = r"Column\(s\) \[1, 'b'\] do not exist"
290-
with pytest.raises(KeyError, match=msg):
291-
df.transform({"a": int, 1: str, "b": int})
292-
293-
294227
def test_transform_empty_dataframe():
295228
# https://github.com/pandas-dev/pandas/issues/39636
296229
df = DataFrame([], columns=["col1", "col2"])

0 commit comments

Comments
 (0)