Skip to content

Commit 0c05fc7

Browse files
topper-123Terji Petersen
authored and
MarcoGorelli
committed
DEPR: remove Int64Index, UInt64Index, Float64Index from tests.indexes.numeric (pandas-dev#49682)
* DEPR: remove Int64Index, UInt64Index, Float64Index from tests.indexes.numeric * add back pylint * RangeIndex in test_indexing.py Co-authored-by: Terji Petersen <[email protected]>
1 parent 7e5a700 commit 0c05fc7

File tree

6 files changed

+151
-299
lines changed

6 files changed

+151
-299
lines changed

pandas/tests/indexes/common.py

+13
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,19 @@ def test_can_hold_identifiers(self, simple_index):
818818
key = idx[0]
819819
assert idx._can_hold_identifiers_and_holds_name(key) is False
820820

821+
def test_view(self, dtype):
822+
index_cls = self._index_cls
823+
824+
idx = index_cls([], dtype=dtype, name="Foo")
825+
idx_view = idx.view()
826+
assert idx_view.name == "Foo"
827+
828+
idx_view = idx.view(dtype)
829+
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)
830+
831+
idx_view = idx.view(index_cls)
832+
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)
833+
821834
def test_format(self, simple_index):
822835
# GH35439
823836
idx = simple_index

pandas/tests/indexes/numeric/test_astype.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,67 @@
77

88
from pandas import Index
99
import pandas._testing as tm
10-
from pandas.core.indexes.api import (
11-
Float64Index,
12-
Int64Index,
13-
UInt64Index,
14-
)
1510

1611

1712
class TestAstype:
1813
def test_astype_float64_to_uint64(self):
1914
# GH#45309 used to incorrectly return Int64Index
20-
idx = Float64Index([0.0, 5.0, 10.0, 15.0, 20.0])
15+
idx = Index([0.0, 5.0, 10.0, 15.0, 20.0], dtype=np.float64)
2116
result = idx.astype("u8")
22-
expected = UInt64Index([0, 5, 10, 15, 20])
23-
tm.assert_index_equal(result, expected)
17+
expected = Index([0, 5, 10, 15, 20], dtype=np.uint64)
18+
tm.assert_index_equal(result, expected, exact=True)
2419

2520
idx_with_negatives = idx - 10
2621
with pytest.raises(ValueError, match="losslessly"):
2722
idx_with_negatives.astype(np.uint64)
2823

2924
def test_astype_float64_to_object(self):
30-
float_index = Float64Index([0.0, 2.5, 5.0, 7.5, 10.0])
25+
float_index = Index([0.0, 2.5, 5.0, 7.5, 10.0], dtype=np.float64)
3126
result = float_index.astype(object)
3227
assert result.equals(float_index)
3328
assert float_index.equals(result)
34-
assert isinstance(result, Index) and not isinstance(result, Float64Index)
29+
assert isinstance(result, Index) and result.dtype == object
3530

3631
def test_astype_float64_mixed_to_object(self):
3732
# mixed int-float
38-
idx = Float64Index([1.5, 2, 3, 4, 5])
33+
idx = Index([1.5, 2, 3, 4, 5], dtype=np.float64)
3934
idx.name = "foo"
4035
result = idx.astype(object)
4136
assert result.equals(idx)
4237
assert idx.equals(result)
43-
assert isinstance(result, Index) and not isinstance(result, Float64Index)
38+
assert isinstance(result, Index) and result.dtype == object
4439

4540
@pytest.mark.parametrize("dtype", ["int16", "int32", "int64"])
4641
def test_astype_float64_to_int_dtype(self, dtype):
4742
# GH#12881
4843
# a float astype int
49-
idx = Float64Index([0, 1, 2])
44+
idx = Index([0, 1, 2], dtype=np.float64)
5045
result = idx.astype(dtype)
51-
expected = Int64Index([0, 1, 2])
52-
tm.assert_index_equal(result, expected)
46+
expected = Index([0, 1, 2], dtype=np.int64)
47+
tm.assert_index_equal(result, expected, exact=True)
5348

54-
idx = Float64Index([0, 1.1, 2])
49+
idx = Index([0, 1.1, 2], dtype=np.float64)
5550
result = idx.astype(dtype)
56-
expected = Int64Index([0, 1, 2])
57-
tm.assert_index_equal(result, expected)
51+
expected = Index([0, 1, 2], dtype=dtype)
52+
tm.assert_index_equal(result, expected, exact=True)
5853

5954
@pytest.mark.parametrize("dtype", ["float32", "float64"])
6055
def test_astype_float64_to_float_dtype(self, dtype):
6156
# GH#12881
6257
# a float astype int
63-
idx = Float64Index([0, 1, 2])
58+
idx = Index([0, 1, 2], dtype=np.float64)
6459
result = idx.astype(dtype)
6560
expected = idx
66-
tm.assert_index_equal(result, expected)
61+
tm.assert_index_equal(result, expected, exact=True)
6762

68-
idx = Float64Index([0, 1.1, 2])
63+
idx = Index([0, 1.1, 2], dtype=np.float64)
6964
result = idx.astype(dtype)
7065
expected = Index(idx.values.astype(dtype))
71-
tm.assert_index_equal(result, expected)
66+
tm.assert_index_equal(result, expected, exact=True)
7267

7368
@pytest.mark.parametrize("dtype", ["M8[ns]", "m8[ns]"])
7469
def test_cannot_cast_to_datetimelike(self, dtype):
75-
idx = Float64Index([0, 1.1, 2])
70+
idx = Index([0, 1.1, 2], dtype=np.float64)
7671

7772
msg = (
7873
f"Cannot convert Float64Index to dtype {pandas_dtype(dtype)}; "
@@ -85,7 +80,7 @@ def test_cannot_cast_to_datetimelike(self, dtype):
8580
@pytest.mark.parametrize("non_finite", [np.inf, np.nan])
8681
def test_cannot_cast_inf_to_int(self, non_finite, dtype):
8782
# GH#13149
88-
idx = Float64Index([1, 2, non_finite])
83+
idx = Index([1, 2, non_finite], dtype=np.float64)
8984

9085
msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
9186
with pytest.raises(ValueError, match=msg):
@@ -94,6 +89,6 @@ def test_cannot_cast_inf_to_int(self, non_finite, dtype):
9489
def test_astype_from_object(self):
9590
index = Index([1.0, np.nan, 0.2], dtype="object")
9691
result = index.astype(float)
97-
expected = Float64Index([1.0, np.nan, 0.2])
92+
expected = Index([1.0, np.nan, 0.2], dtype=np.float64)
9893
assert result.dtype == expected.dtype
9994
tm.assert_index_equal(result, expected)

pandas/tests/indexes/numeric/test_indexing.py

+47-50
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@
1010
Timestamp,
1111
)
1212
import pandas._testing as tm
13-
from pandas.core.indexes.api import (
14-
Float64Index,
15-
Int64Index,
16-
UInt64Index,
17-
)
1813

1914

2015
@pytest.fixture
2116
def index_large():
22-
# large values used in UInt64Index tests where no compat needed with Int64/Float64
17+
# large values used in Index[uint64] tests where no compat needed with Int64/Float64
2318
large = [2**63, 2**63 + 10, 2**63 + 15, 2**63 + 20, 2**63 + 25]
24-
return UInt64Index(large)
19+
return Index(large, dtype=np.uint64)
2520

2621

2722
class TestGetLoc:
@@ -86,7 +81,7 @@ def test_get_loc_raises_missized_tolerance(self):
8681

8782
@pytest.mark.filterwarnings("ignore:Passing method:FutureWarning")
8883
def test_get_loc_float64(self):
89-
idx = Float64Index([0.0, 1.0, 2.0])
84+
idx = Index([0.0, 1.0, 2.0], dtype=np.float64)
9085
for method in [None, "pad", "backfill", "nearest"]:
9186
assert idx.get_loc(1, method) == 1
9287
if method is not None:
@@ -119,27 +114,27 @@ def test_get_loc_float64(self):
119114
idx.get_loc(1.4, method="nearest", tolerance=np.array([1, 2]))
120115

121116
def test_get_loc_na(self):
122-
idx = Float64Index([np.nan, 1, 2])
117+
idx = Index([np.nan, 1, 2], dtype=np.float64)
123118
assert idx.get_loc(1) == 1
124119
assert idx.get_loc(np.nan) == 0
125120

126-
idx = Float64Index([np.nan, 1, np.nan])
121+
idx = Index([np.nan, 1, np.nan], dtype=np.float64)
127122
assert idx.get_loc(1) == 1
128123

129124
# representable by slice [0:2:2]
130125
msg = "'Cannot get left slice bound for non-unique label: nan'"
131126
with pytest.raises(KeyError, match=msg):
132127
idx.slice_locs(np.nan)
133128
# not representable by slice
134-
idx = Float64Index([np.nan, 1, np.nan, np.nan])
129+
idx = Index([np.nan, 1, np.nan, np.nan], dtype=np.float64)
135130
assert idx.get_loc(1) == 1
136131
msg = "'Cannot get left slice bound for non-unique label: nan"
137132
with pytest.raises(KeyError, match=msg):
138133
idx.slice_locs(np.nan)
139134

140135
def test_get_loc_missing_nan(self):
141136
# GH#8569
142-
idx = Float64Index([1, 2])
137+
idx = Index([1, 2], dtype=np.float64)
143138
assert idx.get_loc(1) == 0
144139
with pytest.raises(KeyError, match=r"^3$"):
145140
idx.get_loc(3)
@@ -285,14 +280,16 @@ def test_get_indexer_nearest_decreasing(self, method, expected):
285280
actual = index.get_indexer([0.2, 1.8, 8.5], method=method)
286281
tm.assert_numpy_array_equal(actual, np.array(expected, dtype=np.intp))
287282

288-
@pytest.mark.parametrize(
289-
"idx_class", [Int64Index, RangeIndex, Float64Index, UInt64Index]
290-
)
283+
@pytest.mark.parametrize("idx_dtype", ["int64", "float64", "uint64", "range"])
291284
@pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"])
292-
def test_get_indexer_numeric_index_boolean_target(self, method, idx_class):
285+
def test_get_indexer_numeric_index_boolean_target(self, method, idx_dtype):
293286
# GH 16877
294287

295-
numeric_index = idx_class(RangeIndex(4))
288+
if idx_dtype == "range":
289+
numeric_index = RangeIndex(4)
290+
else:
291+
numeric_index = Index(np.arange(4, dtype=idx_dtype))
292+
296293
other = Index([True, False, True])
297294

298295
result = getattr(numeric_index, method)(other)
@@ -336,7 +333,7 @@ def test_get_indexer_numeric_vs_bool(self):
336333
tm.assert_numpy_array_equal(res, expected)
337334

338335
def test_get_indexer_float64(self):
339-
idx = Float64Index([0.0, 1.0, 2.0])
336+
idx = Index([0.0, 1.0, 2.0], dtype=np.float64)
340337
tm.assert_numpy_array_equal(
341338
idx.get_indexer(idx), np.array([0, 1, 2], dtype=np.intp)
342339
)
@@ -354,39 +351,39 @@ def test_get_indexer_float64(self):
354351

355352
def test_get_indexer_nan(self):
356353
# GH#7820
357-
result = Float64Index([1, 2, np.nan]).get_indexer([np.nan])
354+
result = Index([1, 2, np.nan], dtype=np.float64).get_indexer([np.nan])
358355
expected = np.array([2], dtype=np.intp)
359356
tm.assert_numpy_array_equal(result, expected)
360357

361358
def test_get_indexer_int64(self):
362-
index = Int64Index(range(0, 20, 2))
363-
target = Int64Index(np.arange(10))
359+
index = Index(range(0, 20, 2), dtype=np.int64)
360+
target = Index(np.arange(10), dtype=np.int64)
364361
indexer = index.get_indexer(target)
365362
expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
366363
tm.assert_numpy_array_equal(indexer, expected)
367364

368-
target = Int64Index(np.arange(10))
365+
target = Index(np.arange(10), dtype=np.int64)
369366
indexer = index.get_indexer(target, method="pad")
370367
expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
371368
tm.assert_numpy_array_equal(indexer, expected)
372369

373-
target = Int64Index(np.arange(10))
370+
target = Index(np.arange(10), dtype=np.int64)
374371
indexer = index.get_indexer(target, method="backfill")
375372
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
376373
tm.assert_numpy_array_equal(indexer, expected)
377374

378375
def test_get_indexer_uint64(self, index_large):
379-
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
376+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
380377
indexer = index_large.get_indexer(target)
381378
expected = np.array([0, -1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
382379
tm.assert_numpy_array_equal(indexer, expected)
383380

384-
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
381+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
385382
indexer = index_large.get_indexer(target, method="pad")
386383
expected = np.array([0, 0, 1, 2, 3, 4, 4, 4, 4, 4], dtype=np.intp)
387384
tm.assert_numpy_array_equal(indexer, expected)
388385

389-
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
386+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
390387
indexer = index_large.get_indexer(target, method="backfill")
391388
expected = np.array([0, 1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
392389
tm.assert_numpy_array_equal(indexer, expected)
@@ -396,9 +393,9 @@ class TestWhere:
396393
@pytest.mark.parametrize(
397394
"index",
398395
[
399-
Float64Index(np.arange(5, dtype="float64")),
400-
Int64Index(range(0, 20, 2)),
401-
UInt64Index(np.arange(5, dtype="uint64")),
396+
Index(np.arange(5, dtype="float64")),
397+
Index(range(0, 20, 2), dtype=np.int64),
398+
Index(np.arange(5, dtype="uint64")),
402399
],
403400
)
404401
def test_where(self, listlike_box, index):
@@ -407,16 +404,16 @@ def test_where(self, listlike_box, index):
407404
result = index.where(listlike_box(cond))
408405

409406
cond = [False] + [True] * (len(index) - 1)
410-
expected = Float64Index([index._na_value] + index[1:].tolist())
407+
expected = Index([index._na_value] + index[1:].tolist(), dtype=np.float64)
411408
result = index.where(listlike_box(cond))
412409
tm.assert_index_equal(result, expected)
413410

414411
def test_where_uint64(self):
415-
idx = UInt64Index([0, 6, 2])
412+
idx = Index([0, 6, 2], dtype=np.uint64)
416413
mask = np.array([False, True, False])
417414
other = np.array([1], dtype=np.int64)
418415

419-
expected = UInt64Index([1, 6, 1])
416+
expected = Index([1, 6, 1], dtype=np.uint64)
420417

421418
result = idx.where(mask, other)
422419
tm.assert_index_equal(result, expected)
@@ -437,27 +434,27 @@ def test_where_infers_type_instead_of_trying_to_convert_string_to_float(self):
437434

438435

439436
class TestTake:
440-
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])
441-
def test_take_preserve_name(self, klass):
442-
index = klass([1, 2, 3, 4], name="foo")
437+
@pytest.mark.parametrize("idx_dtype", [np.float64, np.int64, np.uint64])
438+
def test_take_preserve_name(self, idx_dtype):
439+
index = Index([1, 2, 3, 4], dtype=idx_dtype, name="foo")
443440
taken = index.take([3, 0, 1])
444441
assert index.name == taken.name
445442

446443
def test_take_fill_value_float64(self):
447444
# GH 12631
448-
idx = Float64Index([1.0, 2.0, 3.0], name="xxx")
445+
idx = Index([1.0, 2.0, 3.0], name="xxx", dtype=np.float64)
449446
result = idx.take(np.array([1, 0, -1]))
450-
expected = Float64Index([2.0, 1.0, 3.0], name="xxx")
447+
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
451448
tm.assert_index_equal(result, expected)
452449

453450
# fill_value
454451
result = idx.take(np.array([1, 0, -1]), fill_value=True)
455-
expected = Float64Index([2.0, 1.0, np.nan], name="xxx")
452+
expected = Index([2.0, 1.0, np.nan], dtype=np.float64, name="xxx")
456453
tm.assert_index_equal(result, expected)
457454

458455
# allow_fill=False
459456
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
460-
expected = Float64Index([2.0, 1.0, 3.0], name="xxx")
457+
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
461458
tm.assert_index_equal(result, expected)
462459

463460
msg = (
@@ -473,15 +470,15 @@ def test_take_fill_value_float64(self):
473470
with pytest.raises(IndexError, match=msg):
474471
idx.take(np.array([1, -5]))
475472

476-
@pytest.mark.parametrize("klass", [Int64Index, UInt64Index])
477-
def test_take_fill_value_ints(self, klass):
473+
@pytest.mark.parametrize("dtype", [np.int64, np.uint64])
474+
def test_take_fill_value_ints(self, dtype):
478475
# see gh-12631
479-
idx = klass([1, 2, 3], name="xxx")
476+
idx = Index([1, 2, 3], dtype=dtype, name="xxx")
480477
result = idx.take(np.array([1, 0, -1]))
481-
expected = klass([2, 1, 3], name="xxx")
478+
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
482479
tm.assert_index_equal(result, expected)
483480

484-
name = klass.__name__
481+
name = type(idx).__name__
485482
msg = f"Unable to fill values because {name} cannot contain NA"
486483

487484
# fill_value=True
@@ -490,7 +487,7 @@ def test_take_fill_value_ints(self, klass):
490487

491488
# allow_fill=False
492489
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
493-
expected = klass([2, 1, 3], name="xxx")
490+
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
494491
tm.assert_index_equal(result, expected)
495492

496493
with pytest.raises(ValueError, match=msg):
@@ -504,18 +501,18 @@ def test_take_fill_value_ints(self, klass):
504501

505502

506503
class TestContains:
507-
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])
508-
def test_contains_none(self, klass):
504+
@pytest.mark.parametrize("dtype", [np.float64, np.int64, np.uint64])
505+
def test_contains_none(self, dtype):
509506
# GH#35788 should return False, not raise TypeError
510-
index = klass([0, 1, 2, 3, 4])
507+
index = Index([0, 1, 2, 3, 4], dtype=dtype)
511508
assert None not in index
512509

513510
def test_contains_float64_nans(self):
514-
index = Float64Index([1.0, 2.0, np.nan])
511+
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
515512
assert np.nan in index
516513

517514
def test_contains_float64_not_nans(self):
518-
index = Float64Index([1.0, 2.0, np.nan])
515+
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
519516
assert 1.0 in index
520517

521518

0 commit comments

Comments
 (0)