@@ -118,21 +118,18 @@ def test_binary_input_aligns_columns(request, dtype_a, dtype_b):
118
118
119
119
if isinstance (dtype_a , dict ) and isinstance (dtype_b , dict ):
120
120
dtype_b ["C" ] = dtype_b .pop ("B" )
121
-
122
121
df2 = pd .DataFrame ({"A" : [1 , 2 ], "C" : [3 , 4 ]}).astype (dtype_b )
123
- with tm .assert_produces_warning (FutureWarning ):
124
- result = np .heaviside (df1 , df2 )
125
- # Expected future behaviour:
126
- # expected = np.heaviside(
127
- # np.array([[1, 3, np.nan], [2, 4, np.nan]]),
128
- # np.array([[1, np.nan, 3], [2, np.nan, 4]]),
129
- # )
130
- # expected = pd.DataFrame(expected, index=[0, 1], columns=["A", "B", "C"])
131
- expected = pd .DataFrame ([[1.0 , 1.0 ], [1.0 , 1.0 ]], columns = ["A" , "B" ])
122
+ # As of 2.0, align first before applying the ufunc
123
+ result = np .heaviside (df1 , df2 )
124
+ expected = np .heaviside (
125
+ np .array ([[1 , 3 , np .nan ], [2 , 4 , np .nan ]]),
126
+ np .array ([[1 , np .nan , 3 ], [2 , np .nan , 4 ]]),
127
+ )
128
+ expected = pd .DataFrame (expected , index = [0 , 1 ], columns = ["A" , "B" , "C" ])
132
129
tm .assert_frame_equal (result , expected )
133
130
134
- # ensure the expected is the same when applying with numpy array
135
131
result = np .heaviside (df1 , df2 .values )
132
+ expected = pd .DataFrame ([[1.0 , 1.0 ], [1.0 , 1.0 ]], columns = ["A" , "B" ])
136
133
tm .assert_frame_equal (result , expected )
137
134
138
135
@@ -146,35 +143,29 @@ def test_binary_input_aligns_index(request, dtype):
146
143
)
147
144
df1 = pd .DataFrame ({"A" : [1 , 2 ], "B" : [3 , 4 ]}, index = ["a" , "b" ]).astype (dtype )
148
145
df2 = pd .DataFrame ({"A" : [1 , 2 ], "B" : [3 , 4 ]}, index = ["a" , "c" ]).astype (dtype )
149
- with tm .assert_produces_warning (FutureWarning ):
150
- result = np .heaviside (df1 , df2 )
151
- # Expected future behaviour:
152
- # expected = np.heaviside(
153
- # np.array([[1, 3], [3, 4], [np.nan, np.nan]]),
154
- # np.array([[1, 3], [np.nan, np.nan], [3, 4]]),
155
- # )
156
- # # TODO(FloatArray): this will be Float64Dtype.
157
- # expected = pd.DataFrame(expected, index=["a", "b", "c"], columns=["A", "B"])
158
- expected = pd .DataFrame (
159
- [[1.0 , 1.0 ], [1.0 , 1.0 ]], columns = ["A" , "B" ], index = ["a" , "b" ]
146
+ result = np .heaviside (df1 , df2 )
147
+ expected = np .heaviside (
148
+ np .array ([[1 , 3 ], [3 , 4 ], [np .nan , np .nan ]]),
149
+ np .array ([[1 , 3 ], [np .nan , np .nan ], [3 , 4 ]]),
160
150
)
151
+ # TODO(FloatArray): this will be Float64Dtype.
152
+ expected = pd .DataFrame (expected , index = ["a" , "b" , "c" ], columns = ["A" , "B" ])
161
153
tm .assert_frame_equal (result , expected )
162
154
163
- # ensure the expected is the same when applying with numpy array
164
155
result = np .heaviside (df1 , df2 .values )
156
+ expected = pd .DataFrame (
157
+ [[1.0 , 1.0 ], [1.0 , 1.0 ]], columns = ["A" , "B" ], index = ["a" , "b" ]
158
+ )
165
159
tm .assert_frame_equal (result , expected )
166
160
167
161
168
- @pytest .mark .filterwarnings ("ignore:Calling a ufunc on non-aligned:FutureWarning" )
169
162
def test_binary_frame_series_raises ():
170
163
# We don't currently implement
171
164
df = pd .DataFrame ({"A" : [1 , 2 ]})
172
- # with pytest.raises(NotImplementedError, match="logaddexp"):
173
- with pytest .raises (ValueError , match = "" ):
165
+ with pytest .raises (NotImplementedError , match = "logaddexp" ):
174
166
np .logaddexp (df , df ["A" ])
175
167
176
- # with pytest.raises(NotImplementedError, match="logaddexp"):
177
- with pytest .raises (ValueError , match = "" ):
168
+ with pytest .raises (NotImplementedError , match = "logaddexp" ):
178
169
np .logaddexp (df ["A" ], df )
179
170
180
171
@@ -206,7 +197,8 @@ def test_frame_outer_disallowed():
206
197
np .subtract .outer (df , df )
207
198
208
199
209
- def test_alignment_deprecation ():
200
+ def test_alignment_deprecation_enforced ():
201
+ # Enforced in 2.0
210
202
# https://github.com/pandas-dev/pandas/issues/39184
211
203
df1 = pd .DataFrame ({"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
212
204
df2 = pd .DataFrame ({"b" : [1 , 2 , 3 ], "c" : [4 , 5 , 6 ]})
@@ -221,12 +213,11 @@ def test_alignment_deprecation():
221
213
result = np .add (df1 , df1 )
222
214
tm .assert_frame_equal (result , expected )
223
215
224
- with tm .assert_produces_warning (FutureWarning ):
225
- # non-aligned -> warns
226
- result = np .add (df1 , df2 )
216
+ result = np .add (df1 , df2 .values )
227
217
tm .assert_frame_equal (result , expected )
228
218
229
- result = np .add (df1 , df2 .values )
219
+ result = np .add (df1 , df2 )
220
+ expected = pd .DataFrame ({"a" : [np .nan ] * 3 , "b" : [5 , 7 , 9 ], "c" : [np .nan ] * 3 })
230
221
tm .assert_frame_equal (result , expected )
231
222
232
223
result = np .add (df1 .values , df2 )
@@ -241,20 +232,23 @@ def test_alignment_deprecation():
241
232
result = np .add (df1 , s1 )
242
233
tm .assert_frame_equal (result , expected )
243
234
244
- with tm .assert_produces_warning (FutureWarning ):
245
- result = np .add (df1 , s2 )
235
+ result = np .add (df1 , s2 .values )
246
236
tm .assert_frame_equal (result , expected )
247
237
248
- with tm .assert_produces_warning (FutureWarning ):
249
- result = np .add (s2 , df1 )
238
+ expected = pd .DataFrame (
239
+ {"a" : [np .nan ] * 3 , "b" : [5.0 , 6.0 , 7.0 ], "c" : [np .nan ] * 3 }
240
+ )
241
+ result = np .add (df1 , s2 )
250
242
tm .assert_frame_equal (result , expected )
251
243
252
- result = np .add (df1 , s2 .values )
253
- tm .assert_frame_equal (result , expected )
244
+ msg = "Cannot apply ufunc <ufunc 'add'> to mixed DataFrame and Series inputs."
245
+ with pytest .raises (NotImplementedError , match = msg ):
246
+ np .add (s2 , df1 )
254
247
255
248
256
249
@td .skip_if_no ("numba" )
257
- def test_alignment_deprecation_many_inputs (request ):
250
+ def test_alignment_deprecation_many_inputs_enforced ():
251
+ # Enforced in 2.0
258
252
# https://github.com/pandas-dev/pandas/issues/39184
259
253
# test that the deprecation also works with > 2 inputs -> using a numba
260
254
# written ufunc for this because numpy itself doesn't have such ufuncs
@@ -271,31 +265,34 @@ def my_ufunc(x, y, z):
271
265
df2 = pd .DataFrame ({"b" : [1 , 2 , 3 ], "c" : [4 , 5 , 6 ]})
272
266
df3 = pd .DataFrame ({"a" : [1 , 2 , 3 ], "c" : [4 , 5 , 6 ]})
273
267
274
- with tm .assert_produces_warning (FutureWarning ):
275
- result = my_ufunc (df1 , df2 , df3 )
276
- expected = pd .DataFrame ([[3.0 , 12.0 ], [6.0 , 15.0 ], [9.0 , 18.0 ]], columns = ["a" , "b" ])
268
+ result = my_ufunc (df1 , df2 , df3 )
269
+ expected = pd .DataFrame (np .full ((3 , 3 ), np .nan ), columns = ["a" , "b" , "c" ])
277
270
tm .assert_frame_equal (result , expected )
278
271
279
272
# all aligned -> no warning
280
273
with tm .assert_produces_warning (None ):
281
274
result = my_ufunc (df1 , df1 , df1 )
275
+ expected = pd .DataFrame ([[3.0 , 12.0 ], [6.0 , 15.0 ], [9.0 , 18.0 ]], columns = ["a" , "b" ])
282
276
tm .assert_frame_equal (result , expected )
283
277
284
278
# mixed frame / arrays
285
- with tm .assert_produces_warning (FutureWarning ):
286
- result = my_ufunc (df1 , df2 , df3 .values )
287
- tm .assert_frame_equal (result , expected )
279
+ msg = (
280
+ r"operands could not be broadcast together with shapes \(3,3\) \(3,3\) \(3,2\)"
281
+ )
282
+ with pytest .raises (ValueError , match = msg ):
283
+ my_ufunc (df1 , df2 , df3 .values )
288
284
289
285
# single frame -> no warning
290
286
with tm .assert_produces_warning (None ):
291
287
result = my_ufunc (df1 , df2 .values , df3 .values )
292
288
tm .assert_frame_equal (result , expected )
293
289
294
290
# takes indices of first frame
295
- with tm .assert_produces_warning (FutureWarning ):
296
- result = my_ufunc (df1 .values , df2 , df3 )
297
- expected = expected .set_axis (["b" , "c" ], axis = 1 )
298
- tm .assert_frame_equal (result , expected )
291
+ msg = (
292
+ r"operands could not be broadcast together with shapes \(3,2\) \(3,3\) \(3,3\)"
293
+ )
294
+ with pytest .raises (ValueError , match = msg ):
295
+ my_ufunc (df1 .values , df2 , df3 )
299
296
300
297
301
298
def test_array_ufuncs_for_many_arguments ():
0 commit comments