Skip to content

Commit f591a4a

Browse files
Terji Petersentopper-123
Terji Petersen
authored andcommitted
remove Int/Uint/Float64Index from tests/arithmetic/conftest.py
1 parent b3d9a90 commit f591a4a

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

pandas/core/ops/missing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ def mask_zero_div_zero(x, y, result: np.ndarray) -> np.ndarray:
128128

129129
if nan_mask.any() or neginf_mask.any() or posinf_mask.any():
130130
# Fill negative/0 with -inf, positive/0 with +inf, 0/0 with NaN
131-
result = result.astype("float64", copy=False)
131+
if not is_float_dtype(result):
132+
result = result.astype("float64", copy=False)
132133

133134
result[nan_mask] = np.nan
134135
result[posinf_mask] = np.inf

pandas/tests/arithmetic/conftest.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
import pandas as pd
55
from pandas import RangeIndex
66
import pandas._testing as tm
7-
from pandas.core.api import (
8-
Float64Index,
9-
Int64Index,
10-
UInt64Index,
11-
)
7+
from pandas.core.api import NumericIndex
128
from pandas.core.computation import expressions as expr
139

1410

@@ -79,23 +75,23 @@ def zero(request):
7975
--------
8076
arr = RangeIndex(5)
8177
arr / zeros
82-
Float64Index([nan, inf, inf, inf, inf], dtype='float64')
78+
NumericIndex([nan, inf, inf, inf, inf], dtype='float64')
8379
"""
8480
return request.param
8581

8682

8783
# ------------------------------------------------------------------
8884
# Vector Fixtures
8985

86+
_numeric_idx_params = [
87+
NumericIndex(np.arange(5, dtype=dtype))
88+
for dtype in [np.int64, np.uint64, np.float64, np.int8, np.float32]
89+
] + [RangeIndex(5)]
90+
9091

9192
@pytest.fixture(
92-
params=[
93-
Float64Index(np.arange(5, dtype="float64")),
94-
Int64Index(np.arange(5, dtype="int64")),
95-
UInt64Index(np.arange(5, dtype="uint64")),
96-
RangeIndex(5),
97-
],
98-
ids=lambda x: type(x).__name__,
93+
params=_numeric_idx_params,
94+
ids=lambda x: f"{type(x).__name__}[{x.dtype}]",
9995
)
10096
def numeric_idx(request):
10197
"""

pandas/tests/arithmetic/test_numeric.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,51 +322,67 @@ def test_add_sub_datetimedeltalike_invalid(
322322

323323

324324
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+
325336
def test_div_zero(self, zero, numeric_idx):
326337
idx = numeric_idx
327338

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)
329341
# We only adjust for Index, because Series does not yet apply
330342
# the adjustment correctly.
331343
expected2 = adjust_negative_zero(zero, expected)
332344

333345
result = idx / zero
334346
tm.assert_index_equal(result, expected2)
335347
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)))
337349

338350
def test_floordiv_zero(self, zero, numeric_idx):
339351
idx = numeric_idx
340352

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)
342355
# We only adjust for Index, because Series does not yet apply
343356
# the adjustment correctly.
344357
expected2 = adjust_negative_zero(zero, expected)
345358

346359
result = idx // zero
347360
tm.assert_index_equal(result, expected2)
348361
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)))
350363

351364
def test_mod_zero(self, zero, numeric_idx):
352365
idx = numeric_idx
353366

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+
355370
result = idx % zero
356371
tm.assert_index_equal(result, expected)
357372
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))
359374

360375
def test_divmod_zero(self, zero, numeric_idx):
361376
idx = numeric_idx
362377

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)
366382

367383
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)
370386

371387
@pytest.mark.parametrize("op", [operator.truediv, operator.floordiv])
372388
def test_div_negative_zero(self, zero, numeric_idx, op):
@@ -375,7 +391,8 @@ def test_div_negative_zero(self, zero, numeric_idx, op):
375391
return
376392
idx = numeric_idx - 3
377393

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)
379396
expected = adjust_negative_zero(zero, expected)
380397

381398
result = op(idx, zero)
@@ -641,7 +658,8 @@ def test_div_equiv_binop(self):
641658
def test_div_int(self, numeric_idx):
642659
idx = numeric_idx
643660
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)
645663
tm.assert_index_equal(result, expected)
646664

647665
result = idx / 2
@@ -663,16 +681,14 @@ def test_mul_int_array(self, numeric_idx):
663681
result = idx * np.array(5, dtype="int64")
664682
tm.assert_index_equal(result, idx * 5)
665683

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)
668685
tm.assert_index_equal(result, didx)
669686

670687
def test_mul_int_series(self, numeric_idx):
671688
idx = numeric_idx
672689
didx = idx * idx
673690

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))
676692
tm.assert_series_equal(result, Series(didx))
677693

678694
def test_mul_float_series(self, numeric_idx):
@@ -708,7 +724,8 @@ def test_pow_float(self, op, numeric_idx, box_with_array):
708724
# test power calculations both ways, GH#14973
709725
box = box_with_array
710726
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)
712729

713730
idx = tm.box_expected(idx, box)
714731
expected = tm.box_expected(expected, box)

0 commit comments

Comments
 (0)