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
910import re
1011
1112import numpy as np
1213import pytest
1314
1415from 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+
3752def 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+
57110def 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+
218295def test_transform_none_to_type ():
219296 # GH#34377
220297 df = DataFrame ({"a" : [None ]})
0 commit comments