Skip to content

Commit ad2ff04

Browse files
authored
TST/CLN: Move raising apply tests to test_invalid_arg (#40213)
1 parent 74a6898 commit ad2ff04

File tree

3 files changed

+78
-63
lines changed

3 files changed

+78
-63
lines changed

pandas/tests/apply/test_frame_apply.py

-9
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ def test_apply(float_frame):
3737
assert result[d] == expected
3838
assert result.index is float_frame.index
3939

40-
# invalid axis
41-
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"])
42-
msg = "No axis named 2 for object type DataFrame"
43-
with pytest.raises(ValueError, match=msg):
44-
df.apply(lambda x: x, 2)
45-
4640
# GH 9573
4741
df = DataFrame({"c0": ["A", "A", "B", "B"], "c1": ["C", "C", "D", "D"]})
4842
result = df.apply(lambda ts: ts.astype("category"))
@@ -579,9 +573,6 @@ def test_applymap_na_ignore(float_frame):
579573
strlen_frame_with_na[mask] = pd.NA
580574
tm.assert_frame_equal(strlen_frame_na_ignore, strlen_frame_with_na)
581575

582-
with pytest.raises(ValueError, match="na_action must be .*Got 'abc'"):
583-
float_frame_with_na.applymap(lambda x: len(str(x)), na_action="abc")
584-
585576

586577
def test_applymap_box_timestamps():
587578
# GH 2689, GH 2627

pandas/tests/apply/test_invalid_arg.py

+78-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
# 4. invalid result shape/type
77
# If your test does not fit into one of these categories, add to this list.
88

9+
from itertools import chain
910
import re
1011

1112
import numpy as np
1213
import pytest
1314

1415
from pandas import (
16+
Categorical,
1517
DataFrame,
1618
Series,
1719
date_range,
@@ -34,6 +36,19 @@ def test_result_type_error(result_type, int_frame_const_col):
3436
df.apply(lambda x: [1, 2, 3], axis=1, result_type=result_type)
3537

3638

39+
def test_apply_invalid_axis_value():
40+
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=["a", "a", "c"])
41+
msg = "No axis named 2 for object type DataFrame"
42+
with pytest.raises(ValueError, match=msg):
43+
df.apply(lambda x: x, 2)
44+
45+
46+
def test_applymap_invalid_na_action(float_frame):
47+
# GH 23803
48+
with pytest.raises(ValueError, match="na_action must be .*Got 'abc'"):
49+
float_frame.applymap(lambda x: len(str(x)), na_action="abc")
50+
51+
3752
def test_agg_raises():
3853
# GH 26513
3954
df = DataFrame({"A": [0, 1], "B": [1, 2]})
@@ -43,6 +58,28 @@ def test_agg_raises():
4358
df.agg()
4459

4560

61+
def test_map_with_invalid_na_action_raises():
62+
# https://github.com/pandas-dev/pandas/issues/32815
63+
s = Series([1, 2, 3])
64+
msg = "na_action must either be 'ignore' or None"
65+
with pytest.raises(ValueError, match=msg):
66+
s.map(lambda x: x, na_action="____")
67+
68+
69+
def test_map_categorical_na_action():
70+
values = Categorical(list("ABBABCD"), categories=list("DCBA"), ordered=True)
71+
s = Series(values, name="XX", index=list("abcdefg"))
72+
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
73+
s.map(lambda x: x, na_action="ignore")
74+
75+
76+
def test_map_datetimetz_na_action():
77+
values = date_range("2011-01-01", "2011-01-02", freq="H").tz_localize("Asia/Tokyo")
78+
s = Series(values, name="XX")
79+
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
80+
s.map(lambda x: x, na_action="ignore")
81+
82+
4683
@pytest.mark.parametrize("box", [DataFrame, Series])
4784
@pytest.mark.parametrize("method", ["apply", "agg", "transform"])
4885
@pytest.mark.parametrize("func", [{"A": {"B": "sum"}}, {"A": {"B": ["sum"]}}])
@@ -54,6 +91,22 @@ def test_nested_renamer(box, method, func):
5491
getattr(obj, method)(func)
5592

5693

94+
def test_series_agg_nested_renamer():
95+
s = Series(range(6), dtype="int64", name="series")
96+
msg = "nested renamer is not supported"
97+
with pytest.raises(SpecificationError, match=msg):
98+
s.agg({"foo": ["min", "max"]})
99+
100+
101+
def test_multiple_aggregators_with_dict_api():
102+
103+
s = Series(range(6), dtype="int64", name="series")
104+
# nested renaming
105+
msg = "nested renamer is not supported"
106+
with pytest.raises(SpecificationError, match=msg):
107+
s.agg({"foo": ["min", "max"], "bar": ["sum", "mean"]})
108+
109+
57110
def test_transform_nested_renamer():
58111
# GH 35964
59112
match = "nested renamer is not supported"
@@ -208,13 +261,37 @@ def transform2(row):
208261
DataFrame([["a", "b"], ["b", "a"]]), [["cumprod", TypeError]]
209262
),
210263
)
211-
def test_agg_cython_table_raises(df, func, expected, axis):
264+
def test_agg_cython_table_raises_frame(df, func, expected, axis):
212265
# GH 21224
213266
msg = "can't multiply sequence by non-int of type 'str'"
214267
with pytest.raises(expected, match=msg):
215268
df.agg(func, axis=axis)
216269

217270

271+
@pytest.mark.parametrize(
272+
"series, func, expected",
273+
chain(
274+
tm.get_cython_table_params(
275+
Series("a b c".split()),
276+
[
277+
("mean", TypeError), # mean raises TypeError
278+
("prod", TypeError),
279+
("std", TypeError),
280+
("var", TypeError),
281+
("median", TypeError),
282+
("cumprod", TypeError),
283+
],
284+
)
285+
),
286+
)
287+
def test_agg_cython_table_raises_series(series, func, expected):
288+
# GH21224
289+
msg = r"[Cc]ould not convert|can't multiply sequence by non-int of type"
290+
with pytest.raises(expected, match=msg):
291+
# e.g. Series('a b'.split()).cumprod() will raise
292+
series.agg(func)
293+
294+
218295
def test_transform_none_to_type():
219296
# GH#34377
220297
df = DataFrame({"a": [None]})

pandas/tests/apply/test_series_apply.py

-53
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
timedelta_range,
2121
)
2222
import pandas._testing as tm
23-
from pandas.core.base import SpecificationError
2423

2524

2625
def test_series_map_box_timedelta():
@@ -269,20 +268,6 @@ def test_demo():
269268
expected = Series([0], index=["foo"], name="series")
270269
tm.assert_series_equal(result, expected)
271270

272-
# nested renaming
273-
msg = "nested renamer is not supported"
274-
with pytest.raises(SpecificationError, match=msg):
275-
s.agg({"foo": ["min", "max"]})
276-
277-
278-
def test_multiple_aggregators_with_dict_api():
279-
280-
s = Series(range(6), dtype="int64", name="series")
281-
# nested renaming
282-
msg = "nested renamer is not supported"
283-
with pytest.raises(SpecificationError, match=msg):
284-
s.agg({"foo": ["min", "max"], "bar": ["sum", "mean"]})
285-
286271

287272
def test_agg_apply_evaluate_lambdas_the_same(string_series):
288273
# test that we are evaluating row-by-row first
@@ -439,30 +424,6 @@ def test_agg_cython_table_transform(series, func, expected):
439424
tm.assert_series_equal(result, expected)
440425

441426

442-
@pytest.mark.parametrize(
443-
"series, func, expected",
444-
chain(
445-
tm.get_cython_table_params(
446-
Series("a b c".split()),
447-
[
448-
("mean", TypeError), # mean raises TypeError
449-
("prod", TypeError),
450-
("std", TypeError),
451-
("var", TypeError),
452-
("median", TypeError),
453-
("cumprod", TypeError),
454-
],
455-
)
456-
),
457-
)
458-
def test_agg_cython_table_raises(series, func, expected):
459-
# GH21224
460-
msg = r"[Cc]ould not convert|can't multiply sequence by non-int of type"
461-
with pytest.raises(expected, match=msg):
462-
# e.g. Series('a b'.split()).cumprod() will raise
463-
series.agg(func)
464-
465-
466427
def test_series_apply_no_suffix_index():
467428
# GH36189
468429
s = Series([4] * 3)
@@ -732,9 +693,6 @@ def test_map_categorical():
732693
tm.assert_series_equal(result, exp)
733694
assert result.dtype == object
734695

735-
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
736-
s.map(lambda x: x, na_action="ignore")
737-
738696

739697
def test_map_datetimetz():
740698
values = pd.date_range("2011-01-01", "2011-01-02", freq="H").tz_localize(
@@ -756,9 +714,6 @@ def test_map_datetimetz():
756714
exp = Series(list(range(24)) + [0], name="XX", dtype=np.int64)
757715
tm.assert_series_equal(result, exp)
758716

759-
with pytest.raises(NotImplementedError, match=tm.EMPTY_STRING_PATTERN):
760-
s.map(lambda x: x, na_action="ignore")
761-
762717
# not vectorized
763718
def f(x):
764719
if not isinstance(x, pd.Timestamp):
@@ -827,14 +782,6 @@ def test_map_float_to_string_precision():
827782
assert result == expected
828783

829784

830-
def test_map_with_invalid_na_action_raises():
831-
# https://github.com/pandas-dev/pandas/issues/32815
832-
s = Series([1, 2, 3])
833-
msg = "na_action must either be 'ignore' or None"
834-
with pytest.raises(ValueError, match=msg):
835-
s.map(lambda x: x, na_action="____")
836-
837-
838785
def test_apply_to_timedelta():
839786
list_of_valid_strings = ["00:00:01", "00:00:02"]
840787
a = pd.to_timedelta(list_of_valid_strings)

0 commit comments

Comments
 (0)