@@ -322,51 +322,67 @@ def test_add_sub_datetimedeltalike_invalid(
322
322
323
323
324
324
class TestDivisionByZero :
325
+ def _get_expected_dtype (self , idx , zero ):
326
+ """Helper function for getting the correct return dtype.
327
+
328
+ The return dtype for floats depends on the if zero is shaped or not.
329
+ """
330
+ if not tm .is_float_dtype (idx ) or (hasattr (zero , "shape" ) and zero .shape ):
331
+ expected_dtype = np .float64
332
+ else :
333
+ expected_dtype = idx .dtype
334
+ return expected_dtype
335
+
325
336
def test_div_zero (self , zero , numeric_idx ):
326
337
idx = numeric_idx
327
338
328
- expected = Index ([np .nan , np .inf , np .inf , np .inf , np .inf ], dtype = np .float64 )
339
+ expected_dtype = self ._get_expected_dtype (idx , zero = zero )
340
+ expected = Index ([np .nan , np .inf , np .inf , np .inf , np .inf ], dtype = expected_dtype )
329
341
# We only adjust for Index, because Series does not yet apply
330
342
# the adjustment correctly.
331
343
expected2 = adjust_negative_zero (zero , expected )
332
344
333
345
result = idx / zero
334
346
tm .assert_index_equal (result , expected2 )
335
347
ser_compat = Series (idx ).astype ("i8" ) / np .array (zero ).astype ("i8" )
336
- tm .assert_series_equal (ser_compat , Series (expected ))
348
+ tm .assert_series_equal (ser_compat , Series (expected . astype ( np . float64 ) ))
337
349
338
350
def test_floordiv_zero (self , zero , numeric_idx ):
339
351
idx = numeric_idx
340
352
341
- expected = Index ([np .nan , np .inf , np .inf , np .inf , np .inf ], dtype = np .float64 )
353
+ expected_dtype = self ._get_expected_dtype (idx , zero = zero )
354
+ expected = Index ([np .nan , np .inf , np .inf , np .inf , np .inf ], dtype = expected_dtype )
342
355
# We only adjust for Index, because Series does not yet apply
343
356
# the adjustment correctly.
344
357
expected2 = adjust_negative_zero (zero , expected )
345
358
346
359
result = idx // zero
347
360
tm .assert_index_equal (result , expected2 )
348
361
ser_compat = Series (idx ).astype ("i8" ) // np .array (zero ).astype ("i8" )
349
- tm .assert_series_equal (ser_compat , Series (expected ))
362
+ tm .assert_series_equal (ser_compat , Series (expected . astype ( np . float64 ) ))
350
363
351
364
def test_mod_zero (self , zero , numeric_idx ):
352
365
idx = numeric_idx
353
366
354
- expected = Index ([np .nan , np .nan , np .nan , np .nan , np .nan ], dtype = np .float64 )
367
+ expected_dtype = self ._get_expected_dtype (idx , zero = zero )
368
+ expected = Index ([np .nan , np .nan , np .nan , np .nan , np .nan ], dtype = expected_dtype )
369
+
355
370
result = idx % zero
356
371
tm .assert_index_equal (result , expected )
357
372
ser_compat = Series (idx ).astype ("i8" ) % np .array (zero ).astype ("i8" )
358
- tm .assert_series_equal (ser_compat , Series (result ))
373
+ tm .assert_series_equal (ser_compat , Series (result , dtype = np . float64 ))
359
374
360
375
def test_divmod_zero (self , zero , numeric_idx ):
361
376
idx = numeric_idx
362
377
363
- exleft = Index ([np .nan , np .inf , np .inf , np .inf , np .inf ], dtype = np .float64 )
364
- exright = Index ([np .nan , np .nan , np .nan , np .nan , np .nan ], dtype = np .float64 )
365
- exleft = adjust_negative_zero (zero , exleft )
378
+ expe_dtype = self ._get_expected_dtype (idx , zero = zero )
379
+ exp_left = Index ([np .nan , np .inf , np .inf , np .inf , np .inf ], dtype = expe_dtype )
380
+ exp_right = Index ([np .nan , np .nan , np .nan , np .nan , np .nan ], dtype = expe_dtype )
381
+ exp_left = adjust_negative_zero (zero , exp_left )
366
382
367
383
result = divmod (idx , zero )
368
- tm .assert_index_equal (result [0 ], exleft )
369
- tm .assert_index_equal (result [1 ], exright )
384
+ tm .assert_index_equal (result [0 ], exp_left )
385
+ tm .assert_index_equal (result [1 ], exp_right )
370
386
371
387
@pytest .mark .parametrize ("op" , [operator .truediv , operator .floordiv ])
372
388
def test_div_negative_zero (self , zero , numeric_idx , op ):
@@ -375,7 +391,8 @@ def test_div_negative_zero(self, zero, numeric_idx, op):
375
391
return
376
392
idx = numeric_idx - 3
377
393
378
- expected = Index ([- np .inf , - np .inf , - np .inf , np .nan , np .inf ], dtype = np .float64 )
394
+ exp_dtype = self ._get_expected_dtype (idx , zero = zero )
395
+ expected = Index ([- np .inf , - np .inf , - np .inf , np .nan , np .inf ], dtype = exp_dtype )
379
396
expected = adjust_negative_zero (zero , expected )
380
397
381
398
result = op (idx , zero )
@@ -641,7 +658,8 @@ def test_div_equiv_binop(self):
641
658
def test_div_int (self , numeric_idx ):
642
659
idx = numeric_idx
643
660
result = idx / 1
644
- expected = idx .astype ("float64" )
661
+ expected_dtype = idx .dtype if tm .is_float_dtype (idx ) else np .float64
662
+ expected = idx .astype (expected_dtype )
645
663
tm .assert_index_equal (result , expected )
646
664
647
665
result = idx / 2
@@ -663,16 +681,14 @@ def test_mul_int_array(self, numeric_idx):
663
681
result = idx * np .array (5 , dtype = "int64" )
664
682
tm .assert_index_equal (result , idx * 5 )
665
683
666
- arr_dtype = "uint64" if tm .is_unsigned_integer_dtype (idx ) else "int64"
667
- result = idx * np .arange (5 , dtype = arr_dtype )
684
+ result = idx * np .arange (5 , dtype = idx .dtype )
668
685
tm .assert_index_equal (result , didx )
669
686
670
687
def test_mul_int_series (self , numeric_idx ):
671
688
idx = numeric_idx
672
689
didx = idx * idx
673
690
674
- arr_dtype = "uint64" if tm .is_unsigned_integer_dtype (idx ) else "int64"
675
- result = idx * Series (np .arange (5 , dtype = arr_dtype ))
691
+ result = idx * Series (np .arange (5 , dtype = idx .dtype ))
676
692
tm .assert_series_equal (result , Series (didx ))
677
693
678
694
def test_mul_float_series (self , numeric_idx ):
@@ -708,7 +724,8 @@ def test_pow_float(self, op, numeric_idx, box_with_array):
708
724
# test power calculations both ways, GH#14973
709
725
box = box_with_array
710
726
idx = numeric_idx
711
- expected = NumericIndex (op (idx .values , 2.0 ), dtype = np .float64 )
727
+ expected_dtype = idx .dtype if tm .is_float_dtype (idx ) else np .float64
728
+ expected = NumericIndex (op (idx .values , 2.0 ), dtype = expected_dtype )
712
729
713
730
idx = tm .box_expected (idx , box )
714
731
expected = tm .box_expected (expected , box )
0 commit comments