@@ -187,7 +187,7 @@ def test_api_compat_before_use(attr):
187187 getattr (rs , attr )
188188
189189
190- def tests_raises_on_nuisance (test_frame ):
190+ def tests_raises_on_nuisance (test_frame , using_infer_string ):
191191 df = test_frame
192192 df ["D" ] = "foo"
193193 r = df .resample ("h" )
@@ -197,6 +197,8 @@ def tests_raises_on_nuisance(test_frame):
197197
198198 expected = r [["A" , "B" , "C" ]].mean ()
199199 msg = re .escape ("agg function failed [how->mean,dtype->" )
200+ if using_infer_string :
201+ msg = "str dtype does not support mean operations"
200202 with pytest .raises (TypeError , match = msg ):
201203 r .mean ()
202204 result = r .mean (numeric_only = True )
@@ -881,7 +883,9 @@ def test_end_and_end_day_origin(
881883 ("sem" , lib .no_default , "could not convert string to float" ),
882884 ],
883885)
884- def test_frame_downsample_method (method , numeric_only , expected_data ):
886+ def test_frame_downsample_method (
887+ method , numeric_only , expected_data , using_infer_string
888+ ):
885889 # GH#46442 test if `numeric_only` behave as expected for DataFrameGroupBy
886890
887891 index = date_range ("2018-01-01" , periods = 2 , freq = "D" )
@@ -898,11 +902,21 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
898902 if method in ("var" , "mean" , "median" , "prod" ):
899903 klass = TypeError
900904 msg = re .escape (f"agg function failed [how->{ method } ,dtype->" )
905+ if using_infer_string :
906+ msg = f"str dtype does not support { method } operations"
907+ elif method in ["sum" , "std" , "sem" ] and using_infer_string :
908+ klass = TypeError
909+ msg = f"str dtype does not support { method } operations"
901910 else :
902911 klass = ValueError
903912 msg = expected_data
904913 with pytest .raises (klass , match = msg ):
905914 _ = func (** kwargs )
915+ elif method == "sum" and using_infer_string and numeric_only is not True :
916+ klass = TypeError
917+ msg = "str dtype does not support sum operations"
918+ with pytest .raises (klass , match = msg ):
919+ _ = func (** kwargs )
906920 else :
907921 result = func (** kwargs )
908922 expected = DataFrame (expected_data , index = expected_index )
@@ -932,7 +946,9 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
932946 ("last" , lib .no_default , ["cat_2" ]),
933947 ],
934948)
935- def test_series_downsample_method (method , numeric_only , expected_data ):
949+ def test_series_downsample_method (
950+ method , numeric_only , expected_data , using_infer_string
951+ ):
936952 # GH#46442 test if `numeric_only` behave as expected for SeriesGroupBy
937953
938954 index = date_range ("2018-01-01" , periods = 2 , freq = "D" )
@@ -948,8 +964,15 @@ def test_series_downsample_method(method, numeric_only, expected_data):
948964 func (** kwargs )
949965 elif method == "prod" :
950966 msg = re .escape ("agg function failed [how->prod,dtype->" )
967+ if using_infer_string :
968+ msg = "str dtype does not support prod operations"
969+ with pytest .raises (TypeError , match = msg ):
970+ func (** kwargs )
971+ elif method == "sum" and using_infer_string and numeric_only is not True :
972+ msg = "str dtype does not support sum operations"
951973 with pytest .raises (TypeError , match = msg ):
952974 func (** kwargs )
975+
953976 else :
954977 result = func (** kwargs )
955978 expected = Series (expected_data , index = expected_index )
0 commit comments