@@ -187,7 +187,7 @@ def test_api_compat_before_use(attr):
187
187
getattr (rs , attr )
188
188
189
189
190
- def tests_raises_on_nuisance (test_frame ):
190
+ def tests_raises_on_nuisance (test_frame , using_infer_string ):
191
191
df = test_frame
192
192
df ["D" ] = "foo"
193
193
r = df .resample ("h" )
@@ -197,6 +197,8 @@ def tests_raises_on_nuisance(test_frame):
197
197
198
198
expected = r [["A" , "B" , "C" ]].mean ()
199
199
msg = re .escape ("agg function failed [how->mean,dtype->" )
200
+ if using_infer_string :
201
+ msg = "str dtype does not support mean operations"
200
202
with pytest .raises (TypeError , match = msg ):
201
203
r .mean ()
202
204
result = r .mean (numeric_only = True )
@@ -881,7 +883,9 @@ def test_end_and_end_day_origin(
881
883
("sem" , lib .no_default , "could not convert string to float" ),
882
884
],
883
885
)
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
+ ):
885
889
# GH#46442 test if `numeric_only` behave as expected for DataFrameGroupBy
886
890
887
891
index = date_range ("2018-01-01" , periods = 2 , freq = "D" )
@@ -898,11 +902,21 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
898
902
if method in ("var" , "mean" , "median" , "prod" ):
899
903
klass = TypeError
900
904
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"
901
910
else :
902
911
klass = ValueError
903
912
msg = expected_data
904
913
with pytest .raises (klass , match = msg ):
905
914
_ = 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 )
906
920
else :
907
921
result = func (** kwargs )
908
922
expected = DataFrame (expected_data , index = expected_index )
@@ -932,7 +946,9 @@ def test_frame_downsample_method(method, numeric_only, expected_data):
932
946
("last" , lib .no_default , ["cat_2" ]),
933
947
],
934
948
)
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
+ ):
936
952
# GH#46442 test if `numeric_only` behave as expected for SeriesGroupBy
937
953
938
954
index = date_range ("2018-01-01" , periods = 2 , freq = "D" )
@@ -948,8 +964,15 @@ def test_series_downsample_method(method, numeric_only, expected_data):
948
964
func (** kwargs )
949
965
elif method == "prod" :
950
966
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"
951
973
with pytest .raises (TypeError , match = msg ):
952
974
func (** kwargs )
975
+
953
976
else :
954
977
result = func (** kwargs )
955
978
expected = Series (expected_data , index = expected_index )
0 commit comments