6
6
# 4. invalid result shape/type
7
7
# If your test does not fit into one of these categories, add to this list.
8
8
9
+ from itertools import chain
9
10
import re
10
11
11
12
import numpy as np
12
13
import pytest
13
14
14
15
from pandas import (
16
+ Categorical ,
15
17
DataFrame ,
16
18
Series ,
17
19
date_range ,
@@ -34,6 +36,19 @@ def test_result_type_error(result_type, int_frame_const_col):
34
36
df .apply (lambda x : [1 , 2 , 3 ], axis = 1 , result_type = result_type )
35
37
36
38
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
+
37
52
def test_agg_raises ():
38
53
# GH 26513
39
54
df = DataFrame ({"A" : [0 , 1 ], "B" : [1 , 2 ]})
@@ -43,6 +58,28 @@ def test_agg_raises():
43
58
df .agg ()
44
59
45
60
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
+
46
83
@pytest .mark .parametrize ("box" , [DataFrame , Series ])
47
84
@pytest .mark .parametrize ("method" , ["apply" , "agg" , "transform" ])
48
85
@pytest .mark .parametrize ("func" , [{"A" : {"B" : "sum" }}, {"A" : {"B" : ["sum" ]}}])
@@ -54,6 +91,22 @@ def test_nested_renamer(box, method, func):
54
91
getattr (obj , method )(func )
55
92
56
93
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
+
57
110
def test_transform_nested_renamer ():
58
111
# GH 35964
59
112
match = "nested renamer is not supported"
@@ -208,13 +261,37 @@ def transform2(row):
208
261
DataFrame ([["a" , "b" ], ["b" , "a" ]]), [["cumprod" , TypeError ]]
209
262
),
210
263
)
211
- def test_agg_cython_table_raises (df , func , expected , axis ):
264
+ def test_agg_cython_table_raises_frame (df , func , expected , axis ):
212
265
# GH 21224
213
266
msg = "can't multiply sequence by non-int of type 'str'"
214
267
with pytest .raises (expected , match = msg ):
215
268
df .agg (func , axis = axis )
216
269
217
270
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
+
218
295
def test_transform_none_to_type ():
219
296
# GH#34377
220
297
df = DataFrame ({"a" : [None ]})
0 commit comments