77
88import pandas as pd
99import pandas ._testing as tm
10- from pandas .core .arrays import integer_array
10+ from pandas .core .arrays import FloatingArray , integer_array
1111import pandas .core .ops as ops
1212
1313# Basic test for the arithmetic array ops
@@ -45,24 +45,26 @@ def test_sub(dtype):
4545
4646
4747def test_div (dtype ):
48- # for now division gives a float numpy array
4948 a = pd .array ([1 , 2 , 3 , None , 5 ], dtype = dtype )
5049 b = pd .array ([0 , 1 , None , 3 , 4 ], dtype = dtype )
5150
5251 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 )
5554
5655
5756@pytest .mark .parametrize ("zero, negative" , [(0 , False ), (0.0 , False ), (- 0.0 , True )])
5857def test_divide_by_zero (zero , negative ):
5958 # https://github.com/pandas-dev/pandas/issues/27398
6059 a = pd .array ([0 , 1 , - 1 , None ], dtype = "Int64" )
6160 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+ )
6365 if negative :
6466 expected *= - 1
65- tm .assert_numpy_array_equal (result , expected )
67+ tm .assert_extension_array_equal (result , expected )
6668
6769
6870def test_floordiv (dtype ):
@@ -99,8 +101,11 @@ def test_pow_scalar():
99101 tm .assert_extension_array_equal (result , expected )
100102
101103 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 )
104109
105110 # reversed
106111 a = a [1 :] # Can't raise integers to negative powers.
@@ -118,8 +123,11 @@ def test_pow_scalar():
118123 tm .assert_extension_array_equal (result , expected )
119124
120125 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 )
123131
124132
125133def test_pow_array ():
@@ -133,10 +141,10 @@ def test_pow_array():
133141def test_rpow_one_to_na ():
134142 # https://github.com/pandas-dev/pandas/issues/22022
135143 # 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" )
137145 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 )
140148
141149
142150@pytest .mark .parametrize ("other" , [0 , 0.5 ])
@@ -198,11 +206,19 @@ def test_arith_coerce_scalar(data, all_arithmetic_operators):
198206
199207 result = op (s , other )
200208 expected = op (s .astype (float ), other )
209+ expected = expected .astype ("Float64" )
201210 # rfloordiv results in nan instead of inf
202211 if all_arithmetic_operators == "__rfloordiv__" and _np_version_under1p20 :
203212 # for numpy 1.20 https://github.com/numpy/numpy/pull/16161
204213 # 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
206222
207223 tm .assert_series_equal (result , expected )
208224
@@ -215,7 +231,7 @@ def test_arithmetic_conversion(all_arithmetic_operators, other):
215231
216232 s = pd .Series ([1 , 2 , 3 ], dtype = "Int64" )
217233 result = op (s , other )
218- assert result .dtype is np . dtype ( "float" )
234+ assert result .dtype == "Float64"
219235
220236
221237def test_cross_type_arithmetic ():
0 commit comments