5
5
6
6
import pandas as pd
7
7
import pandas ._testing as tm
8
- from pandas .core .arrays import ExtensionArray , integer_array
8
+ from pandas .core .arrays import integer_array
9
9
import pandas .core .ops as ops
10
10
11
-
12
- # TODO need to use existing utility function or move this somewhere central
13
- def get_op_from_name (op_name ):
14
- short_opname = op_name .strip ("_" )
15
- try :
16
- op = getattr (operator , short_opname )
17
- except AttributeError :
18
- # Assume it is the reverse operator
19
- rop = getattr (operator , short_opname [1 :])
20
- op = lambda x , y : rop (y , x )
21
-
22
- return op
23
-
24
-
25
11
# Basic test for the arithmetic array ops
26
12
# -----------------------------------------------------------------------------
27
13
@@ -151,55 +137,6 @@ def test_rpow_one_to_na():
151
137
tm .assert_numpy_array_equal (result , expected )
152
138
153
139
154
- # Test equivalence of scalars, numpy arrays with array ops
155
- # -----------------------------------------------------------------------------
156
-
157
-
158
- def test_array_scalar_like_equivalence (data , all_arithmetic_operators ):
159
- op = get_op_from_name (all_arithmetic_operators )
160
-
161
- scalar = 2
162
- scalar_array = pd .array ([2 ] * len (data ), dtype = data .dtype )
163
-
164
- # TODO also add len-1 array (np.array([2], dtype=data.dtype.numpy_dtype))
165
- for scalar in [2 , data .dtype .type (2 )]:
166
- result = op (data , scalar )
167
- expected = op (data , scalar_array )
168
- if isinstance (expected , ExtensionArray ):
169
- tm .assert_extension_array_equal (result , expected )
170
- else :
171
- # TODO div still gives float ndarray -> remove this once we have Float EA
172
- tm .assert_numpy_array_equal (result , expected )
173
-
174
-
175
- def test_array_NA (data , all_arithmetic_operators ):
176
- if "truediv" in all_arithmetic_operators :
177
- pytest .skip ("division with pd.NA raises" )
178
- op = get_op_from_name (all_arithmetic_operators )
179
-
180
- scalar = pd .NA
181
- scalar_array = pd .array ([pd .NA ] * len (data ), dtype = data .dtype )
182
-
183
- result = op (data , scalar )
184
- expected = op (data , scalar_array )
185
- tm .assert_extension_array_equal (result , expected )
186
-
187
-
188
- def test_numpy_array_equivalence (data , all_arithmetic_operators ):
189
- op = get_op_from_name (all_arithmetic_operators )
190
-
191
- numpy_array = np .array ([2 ] * len (data ), dtype = data .dtype .numpy_dtype )
192
- pd_array = pd .array (numpy_array , dtype = data .dtype )
193
-
194
- result = op (data , numpy_array )
195
- expected = op (data , pd_array )
196
- if isinstance (expected , ExtensionArray ):
197
- tm .assert_extension_array_equal (result , expected )
198
- else :
199
- # TODO div still gives float ndarray -> remove this once we have Float EA
200
- tm .assert_numpy_array_equal (result , expected )
201
-
202
-
203
140
@pytest .mark .parametrize ("other" , [0 , 0.5 ])
204
141
def test_numpy_zero_dim_ndarray (other ):
205
142
arr = integer_array ([1 , None , 2 ])
@@ -208,53 +145,7 @@ def test_numpy_zero_dim_ndarray(other):
208
145
tm .assert_equal (result , expected )
209
146
210
147
211
- # Test equivalence with Series and DataFrame ops
212
- # -----------------------------------------------------------------------------
213
-
214
-
215
- def test_frame (data , all_arithmetic_operators ):
216
- op = get_op_from_name (all_arithmetic_operators )
217
-
218
- # DataFrame with scalar
219
- df = pd .DataFrame ({"A" : data })
220
- scalar = 2
221
-
222
- result = op (df , scalar )
223
- expected = pd .DataFrame ({"A" : op (data , scalar )})
224
- tm .assert_frame_equal (result , expected )
225
-
226
-
227
- def test_series (data , all_arithmetic_operators ):
228
- op = get_op_from_name (all_arithmetic_operators )
229
-
230
- s = pd .Series (data )
231
-
232
- # Series with scalar
233
- scalar = 2
234
- result = op (s , scalar )
235
- expected = pd .Series (op (data , scalar ))
236
- tm .assert_series_equal (result , expected )
237
-
238
- # Series with np.ndarray
239
- other = np .ones (len (data ), dtype = data .dtype .type )
240
- result = op (s , other )
241
- expected = pd .Series (op (data , other ))
242
- tm .assert_series_equal (result , expected )
243
-
244
- # Series with pd.array
245
- other = pd .array (np .ones (len (data )), dtype = data .dtype )
246
- result = op (s , other )
247
- expected = pd .Series (op (data , other ))
248
- tm .assert_series_equal (result , expected )
249
-
250
- # Series with Series
251
- other = pd .Series (np .ones (len (data )), dtype = data .dtype )
252
- result = op (s , other )
253
- expected = pd .Series (op (data , other .array ))
254
- tm .assert_series_equal (result , expected )
255
-
256
-
257
- # Test generic charachteristics / errors
148
+ # Test generic characteristics / errors
258
149
# -----------------------------------------------------------------------------
259
150
260
151
@@ -291,35 +182,6 @@ def test_error_invalid_values(data, all_arithmetic_operators):
291
182
ops (pd .Series (pd .date_range ("20180101" , periods = len (s ))))
292
183
293
184
294
- def test_error_invalid_object (data , all_arithmetic_operators ):
295
-
296
- op = all_arithmetic_operators
297
- opa = getattr (data , op )
298
-
299
- # 2d -> return NotImplemented
300
- result = opa (pd .DataFrame ({"A" : data }))
301
- assert result is NotImplemented
302
-
303
- msg = r"can only perform ops with 1-d structures"
304
- with pytest .raises (NotImplementedError , match = msg ):
305
- opa (np .arange (len (data )).reshape (- 1 , len (data )))
306
-
307
-
308
- def test_error_len_mismatch (all_arithmetic_operators ):
309
- # operating with a list-like with non-matching length raises
310
- op = get_op_from_name (all_arithmetic_operators )
311
-
312
- data = pd .array ([1 , 2 , 3 ], dtype = "Int64" )
313
-
314
- for other in [[1 , 2 ], np .array ([1.0 , 2.0 ])]:
315
- with pytest .raises (ValueError , match = "Lengths must match" ):
316
- op (data , other )
317
-
318
- s = pd .Series (data )
319
- with pytest .raises (ValueError , match = "Lengths must match" ):
320
- op (s , other )
321
-
322
-
323
185
# Various
324
186
# -----------------------------------------------------------------------------
325
187
@@ -328,7 +190,7 @@ def test_error_len_mismatch(all_arithmetic_operators):
328
190
329
191
330
192
def test_arith_coerce_scalar (data , all_arithmetic_operators ):
331
- op = get_op_from_name (all_arithmetic_operators )
193
+ op = tm . get_op_from_name (all_arithmetic_operators )
332
194
s = pd .Series (data )
333
195
other = 0.01
334
196
@@ -345,7 +207,7 @@ def test_arith_coerce_scalar(data, all_arithmetic_operators):
345
207
def test_arithmetic_conversion (all_arithmetic_operators , other ):
346
208
# if we have a float operand we should have a float result
347
209
# if that is equal to an integer
348
- op = get_op_from_name (all_arithmetic_operators )
210
+ op = tm . get_op_from_name (all_arithmetic_operators )
349
211
350
212
s = pd .Series ([1 , 2 , 3 ], dtype = "Int64" )
351
213
result = op (s , other )
0 commit comments