7
7
8
8
import pandas as pd
9
9
import pandas ._testing as tm
10
- from pandas .core .arrays import integer_array
10
+ from pandas .core .arrays import FloatingArray , integer_array
11
11
import pandas .core .ops as ops
12
12
13
13
# Basic test for the arithmetic array ops
@@ -45,24 +45,26 @@ def test_sub(dtype):
45
45
46
46
47
47
def test_div (dtype ):
48
- # for now division gives a float numpy array
49
48
a = pd .array ([1 , 2 , 3 , None , 5 ], dtype = dtype )
50
49
b = pd .array ([0 , 1 , None , 3 , 4 ], dtype = dtype )
51
50
52
51
result = a / b
53
- expected = np .array ([np .inf , 2 , np . nan , np . nan , 1.25 ], dtype = "float64 " )
54
- tm .assert_numpy_array_equal (result , expected )
52
+ expected = pd .array ([np .inf , 2 , None , None , 1.25 ], dtype = "Float64 " )
53
+ tm .assert_extension_array_equal (result , expected )
55
54
56
55
57
56
@pytest .mark .parametrize ("zero, negative" , [(0 , False ), (0.0 , False ), (- 0.0 , True )])
58
57
def test_divide_by_zero (zero , negative ):
59
58
# https://github.com/pandas-dev/pandas/issues/27398
60
59
a = pd .array ([0 , 1 , - 1 , None ], dtype = "Int64" )
61
60
result = a / zero
62
- expected = np .array ([np .nan , np .inf , - np .inf , np .nan ])
61
+ expected = FloatingArray (
62
+ np .array ([np .nan , np .inf , - np .inf , 1 ], dtype = "float64" ),
63
+ np .array ([False , False , False , True ]),
64
+ )
63
65
if negative :
64
66
expected *= - 1
65
- tm .assert_numpy_array_equal (result , expected )
67
+ tm .assert_extension_array_equal (result , expected )
66
68
67
69
68
70
def test_floordiv (dtype ):
@@ -99,8 +101,11 @@ def test_pow_scalar():
99
101
tm .assert_extension_array_equal (result , expected )
100
102
101
103
result = a ** np .nan
102
- expected = np .array ([np .nan , np .nan , 1 , np .nan , np .nan ], dtype = "float64" )
103
- tm .assert_numpy_array_equal (result , expected )
104
+ expected = FloatingArray (
105
+ np .array ([np .nan , np .nan , 1 , np .nan , np .nan ], dtype = "float64" ),
106
+ np .array ([False , False , False , True , False ]),
107
+ )
108
+ tm .assert_extension_array_equal (result , expected )
104
109
105
110
# reversed
106
111
a = a [1 :] # Can't raise integers to negative powers.
@@ -118,8 +123,11 @@ def test_pow_scalar():
118
123
tm .assert_extension_array_equal (result , expected )
119
124
120
125
result = np .nan ** a
121
- expected = np .array ([1 , np .nan , np .nan , np .nan ], dtype = "float64" )
122
- tm .assert_numpy_array_equal (result , expected )
126
+ expected = FloatingArray (
127
+ np .array ([1 , np .nan , np .nan , np .nan ], dtype = "float64" ),
128
+ np .array ([False , False , True , False ]),
129
+ )
130
+ tm .assert_extension_array_equal (result , expected )
123
131
124
132
125
133
def test_pow_array ():
@@ -133,10 +141,10 @@ def test_pow_array():
133
141
def test_rpow_one_to_na ():
134
142
# https://github.com/pandas-dev/pandas/issues/22022
135
143
# https://github.com/pandas-dev/pandas/issues/29997
136
- arr = integer_array ([np .nan , np .nan ])
144
+ arr = pd . array ([np .nan , np .nan ], dtype = "Int64" )
137
145
result = np .array ([1.0 , 2.0 ]) ** arr
138
- expected = np .array ([1.0 , np .nan ])
139
- tm .assert_numpy_array_equal (result , expected )
146
+ expected = pd .array ([1.0 , np .nan ], dtype = "Float64" )
147
+ tm .assert_extension_array_equal (result , expected )
140
148
141
149
142
150
@pytest .mark .parametrize ("other" , [0 , 0.5 ])
@@ -198,11 +206,19 @@ def test_arith_coerce_scalar(data, all_arithmetic_operators):
198
206
199
207
result = op (s , other )
200
208
expected = op (s .astype (float ), other )
209
+ expected = expected .astype ("Float64" )
201
210
# rfloordiv results in nan instead of inf
202
211
if all_arithmetic_operators == "__rfloordiv__" and _np_version_under1p20 :
203
212
# for numpy 1.20 https://github.com/numpy/numpy/pull/16161
204
213
# updated floordiv, now matches our behavior defined in core.ops
205
- expected [(expected == np .inf ) | (expected == - np .inf )] = np .nan
214
+ mask = (
215
+ ((expected == np .inf ) | (expected == - np .inf )).fillna (False ).to_numpy (bool )
216
+ )
217
+ expected .array ._data [mask ] = np .nan
218
+ # rmod results in NaN that wasn't NA in original nullable Series -> unmask it
219
+ elif all_arithmetic_operators == "__rmod__" :
220
+ mask = (s == 0 ).fillna (False ).to_numpy (bool )
221
+ expected .array ._mask [mask ] = False
206
222
207
223
tm .assert_series_equal (result , expected )
208
224
@@ -215,7 +231,7 @@ def test_arithmetic_conversion(all_arithmetic_operators, other):
215
231
216
232
s = pd .Series ([1 , 2 , 3 ], dtype = "Int64" )
217
233
result = op (s , other )
218
- assert result .dtype is np . dtype ( "float" )
234
+ assert result .dtype == "Float64"
219
235
220
236
221
237
def test_cross_type_arithmetic ():
0 commit comments