@@ -13,22 +13,23 @@ class BaseReduceTests:
13
13
make sense for numeric/boolean operations.
14
14
"""
15
15
16
- def _supports_reduction (self , obj , op_name : str ) -> bool :
16
+ def _supports_reduction (self , ser : pd . Series , op_name : str ) -> bool :
17
17
# Specify if we expect this reduction to succeed.
18
18
return False
19
19
20
- def check_reduce (self , s , op_name , skipna ):
20
+ def check_reduce (self , ser : pd . Series , op_name : str , skipna : bool ):
21
21
# We perform the same operation on the np.float64 data and check
22
22
# that the results match. Override if you need to cast to something
23
23
# other than float64.
24
- res_op = getattr (s , op_name )
24
+ res_op = getattr (ser , op_name )
25
25
26
26
try :
27
- alt = s .astype ("float64" )
28
- except TypeError :
29
- # e.g. Interval can't cast, so let's cast to object and do
27
+ alt = ser .astype ("float64" )
28
+ except (TypeError , ValueError ):
29
+ # e.g. Interval can't cast (TypeError), StringArray can't cast
30
+ # (ValueError), so let's cast to object and do
30
31
# the reduction pointwise
31
- alt = s .astype (object )
32
+ alt = ser .astype (object )
32
33
33
34
exp_op = getattr (alt , op_name )
34
35
if op_name == "count" :
@@ -79,53 +80,53 @@ def check_reduce_frame(self, ser: pd.Series, op_name: str, skipna: bool):
79
80
@pytest .mark .parametrize ("skipna" , [True , False ])
80
81
def test_reduce_series_boolean (self , data , all_boolean_reductions , skipna ):
81
82
op_name = all_boolean_reductions
82
- s = pd .Series (data )
83
+ ser = pd .Series (data )
83
84
84
- if not self ._supports_reduction (s , op_name ):
85
+ if not self ._supports_reduction (ser , op_name ):
85
86
msg = (
86
87
"[Cc]annot perform|Categorical is not ordered for operation|"
87
88
"does not support reduction|"
88
89
)
89
90
90
91
with pytest .raises (TypeError , match = msg ):
91
- getattr (s , op_name )(skipna = skipna )
92
+ getattr (ser , op_name )(skipna = skipna )
92
93
93
94
else :
94
- self .check_reduce (s , op_name , skipna )
95
+ self .check_reduce (ser , op_name , skipna )
95
96
96
97
@pytest .mark .filterwarnings ("ignore::RuntimeWarning" )
97
98
@pytest .mark .parametrize ("skipna" , [True , False ])
98
99
def test_reduce_series_numeric (self , data , all_numeric_reductions , skipna ):
99
100
op_name = all_numeric_reductions
100
- s = pd .Series (data )
101
+ ser = pd .Series (data )
101
102
102
- if not self ._supports_reduction (s , op_name ):
103
+ if not self ._supports_reduction (ser , op_name ):
103
104
msg = (
104
105
"[Cc]annot perform|Categorical is not ordered for operation|"
105
106
"does not support reduction|"
106
107
)
107
108
108
109
with pytest .raises (TypeError , match = msg ):
109
- getattr (s , op_name )(skipna = skipna )
110
+ getattr (ser , op_name )(skipna = skipna )
110
111
111
112
else :
112
113
# min/max with empty produce numpy warnings
113
- self .check_reduce (s , op_name , skipna )
114
+ self .check_reduce (ser , op_name , skipna )
114
115
115
116
@pytest .mark .parametrize ("skipna" , [True , False ])
116
117
def test_reduce_frame (self , data , all_numeric_reductions , skipna ):
117
118
op_name = all_numeric_reductions
118
- s = pd .Series (data )
119
- if not is_numeric_dtype (s .dtype ):
119
+ ser = pd .Series (data )
120
+ if not is_numeric_dtype (ser .dtype ):
120
121
pytest .skip ("not numeric dtype" )
121
122
122
123
if op_name in ["count" , "kurt" , "sem" ]:
123
124
pytest .skip (f"{ op_name } not an array method" )
124
125
125
- if not self ._supports_reduction (s , op_name ):
126
+ if not self ._supports_reduction (ser , op_name ):
126
127
pytest .skip (f"Reduction { op_name } not supported for this dtype" )
127
128
128
- self .check_reduce_frame (s , op_name , skipna )
129
+ self .check_reduce_frame (ser , op_name , skipna )
129
130
130
131
131
132
# TODO: deprecate BaseNoReduceTests, BaseNumericReduceTests, BaseBooleanReduceTests
@@ -135,15 +136,15 @@ class BaseNoReduceTests(BaseReduceTests):
135
136
136
137
class BaseNumericReduceTests (BaseReduceTests ):
137
138
# For backward compatibility only, this only runs the numeric reductions
138
- def _supports_reduction (self , obj , op_name : str ) -> bool :
139
+ def _supports_reduction (self , ser : pd . Series , op_name : str ) -> bool :
139
140
if op_name in ["any" , "all" ]:
140
141
pytest .skip ("These are tested in BaseBooleanReduceTests" )
141
142
return True
142
143
143
144
144
145
class BaseBooleanReduceTests (BaseReduceTests ):
145
146
# For backward compatibility only, this only runs the numeric reductions
146
- def _supports_reduction (self , obj , op_name : str ) -> bool :
147
+ def _supports_reduction (self , ser : pd . Series , op_name : str ) -> bool :
147
148
if op_name not in ["any" , "all" ]:
148
149
pytest .skip ("These are tested in BaseNumericReduceTests" )
149
150
return True
0 commit comments