@@ -133,32 +133,37 @@ def func(x):
133
133
@pytest .mark .parametrize ("op" , [* frame_kernels_raise , lambda x : x + 1 ])
134
134
def test_transform_bad_dtype (op , frame_or_series , request ):
135
135
# GH 35964
136
- if op == "rank" :
137
- request .node .add_marker (
138
- pytest .mark .xfail (
139
- raises = ValueError , reason = "GH 40418: rank does not raise a TypeError"
140
- )
141
- )
142
- elif op == "ngroup" :
136
+ if op == "ngroup" :
143
137
request .node .add_marker (
144
138
pytest .mark .xfail (raises = ValueError , reason = "ngroup not valid for NDFrame" )
145
139
)
146
140
147
141
obj = DataFrame ({"A" : 3 * [object ]}) # DataFrame that will fail on most transforms
148
142
obj = tm .get_obj (obj , frame_or_series )
143
+ if op == "rank" :
144
+ error = ValueError
145
+ msg = "Transform function failed"
146
+ else :
147
+ error = TypeError
148
+ msg = "|" .join (
149
+ [
150
+ "not supported between instances of 'type' and 'type'" ,
151
+ "unsupported operand type" ,
152
+ ]
153
+ )
149
154
150
- with pytest .raises (TypeError , match = "unsupported operand|not supported" ):
155
+ with pytest .raises (error , match = msg ):
151
156
obj .transform (op )
152
- with pytest .raises (TypeError , match = "Transform function failed" ):
157
+ with pytest .raises (error , match = msg ):
153
158
obj .transform ([op ])
154
- with pytest .raises (TypeError , match = "Transform function failed" ):
159
+ with pytest .raises (error , match = msg ):
155
160
obj .transform ({"A" : op })
156
- with pytest .raises (TypeError , match = "Transform function failed" ):
161
+ with pytest .raises (error , match = msg ):
157
162
obj .transform ({"A" : [op ]})
158
163
159
164
160
165
@pytest .mark .parametrize ("op" , frame_kernels_raise )
161
- def test_transform_partial_failure_typeerror (request , op ):
166
+ def test_transform_failure_typeerror (request , op ):
162
167
# GH 35964
163
168
164
169
if op == "ngroup" :
@@ -168,62 +173,52 @@ def test_transform_partial_failure_typeerror(request, op):
168
173
169
174
# Using object makes most transform kernels fail
170
175
df = DataFrame ({"A" : 3 * [object ], "B" : [1 , 2 , 3 ]})
176
+ if op == "rank" :
177
+ error = ValueError
178
+ msg = "Transform function failed"
179
+ else :
180
+ error = TypeError
181
+ msg = "|" .join (
182
+ [
183
+ "not supported between instances of 'type' and 'type'" ,
184
+ "unsupported operand type" ,
185
+ ]
186
+ )
171
187
172
- expected = df [["B" ]].transform ([op ])
173
- match = r"\['A'\] did not transform successfully"
174
- with tm .assert_produces_warning (FutureWarning , match = match ):
175
- result = df .transform ([op ])
176
- tm .assert_equal (result , expected )
188
+ with pytest .raises (error , match = msg ):
189
+ df .transform ([op ])
177
190
178
- expected = df [["B" ]].transform ({"B" : op })
179
- match = r"\['A'\] did not transform successfully"
180
- with tm .assert_produces_warning (FutureWarning , match = match ):
181
- result = df .transform ({"A" : op , "B" : op })
182
- tm .assert_equal (result , expected )
191
+ with pytest .raises (error , match = msg ):
192
+ df .transform ({"A" : op , "B" : op })
183
193
184
- expected = df [["B" ]].transform ({"B" : [op ]})
185
- match = r"\['A'\] did not transform successfully"
186
- with tm .assert_produces_warning (FutureWarning , match = match ):
187
- result = df .transform ({"A" : [op ], "B" : [op ]})
188
- tm .assert_equal (result , expected )
194
+ with pytest .raises (error , match = msg ):
195
+ df .transform ({"A" : [op ], "B" : [op ]})
189
196
190
- expected = df .transform ({"A" : ["shift" ], "B" : [op ]})
191
- match = rf"\['{ op } '\] did not transform successfully"
192
- with tm .assert_produces_warning (FutureWarning , match = match ):
193
- result = df .transform ({"A" : [op , "shift" ], "B" : [op ]})
194
- tm .assert_equal (result , expected )
197
+ with pytest .raises (error , match = msg ):
198
+ df .transform ({"A" : [op , "shift" ], "B" : [op ]})
195
199
196
200
197
- def test_transform_partial_failure_valueerror ():
201
+ def test_transform_failure_valueerror ():
198
202
# GH 40211
199
- match = ".*did not transform successfully"
200
-
201
203
def op (x ):
202
204
if np .sum (np .sum (x )) < 10 :
203
205
raise ValueError
204
206
return x
205
207
206
208
df = DataFrame ({"A" : [1 , 2 , 3 ], "B" : [400 , 500 , 600 ]})
209
+ msg = "Transform function failed"
207
210
208
- expected = df [["B" ]].transform ([op ])
209
- with tm .assert_produces_warning (FutureWarning , match = match ):
210
- result = df .transform ([op ])
211
- tm .assert_equal (result , expected )
211
+ with pytest .raises (ValueError , match = msg ):
212
+ df .transform ([op ])
212
213
213
- expected = df [["B" ]].transform ({"B" : op })
214
- with tm .assert_produces_warning (FutureWarning , match = match ):
215
- result = df .transform ({"A" : op , "B" : op })
216
- tm .assert_equal (result , expected )
214
+ with pytest .raises (ValueError , match = msg ):
215
+ df .transform ({"A" : op , "B" : op })
217
216
218
- expected = df [["B" ]].transform ({"B" : [op ]})
219
- with tm .assert_produces_warning (FutureWarning , match = match ):
220
- result = df .transform ({"A" : [op ], "B" : [op ]})
221
- tm .assert_equal (result , expected )
217
+ with pytest .raises (ValueError , match = msg ):
218
+ df .transform ({"A" : [op ], "B" : [op ]})
222
219
223
- expected = df .transform ({"A" : ["shift" ], "B" : [op ]})
224
- with tm .assert_produces_warning (FutureWarning , match = match ):
225
- result = df .transform ({"A" : [op , "shift" ], "B" : [op ]})
226
- tm .assert_equal (result , expected )
220
+ with pytest .raises (ValueError , match = msg ):
221
+ df .transform ({"A" : [op , "shift" ], "B" : [op ]})
227
222
228
223
229
224
@pytest .mark .parametrize ("use_apply" , [True , False ])
0 commit comments