Skip to content

Commit f423202

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
DEPR: remove Int64Index, UInt64Index, Float64Index from tests.indexes.numeric
1 parent c7010a7 commit f423202

File tree

7 files changed

+147
-304
lines changed

7 files changed

+147
-304
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ repos:
5959
- flake8==5.0.4
6060
- flake8-bugbear==22.7.1
6161
- pandas-dev-flaker==0.5.0
62-
- repo: https://github.com/pycqa/pylint
63-
rev: v2.15.5
64-
hooks:
65-
- id: pylint
66-
stages: [manual]
6762
- repo: https://github.com/PyCQA/isort
6863
rev: 5.10.1
6964
hooks:

pandas/tests/indexes/common.py

Lines changed: 13 additions & 0 deletions
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

Lines changed: 20 additions & 25 deletions
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

Lines changed: 43 additions & 50 deletions
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,12 @@ 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", [np.int64, np.float64, np.uint64])
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+
numeric_index = Index(RangeIndex(4), dtype=idx_dtype)
296289
other = Index([True, False, True])
297290

298291
result = getattr(numeric_index, method)(other)
@@ -336,7 +329,7 @@ def test_get_indexer_numeric_vs_bool(self):
336329
tm.assert_numpy_array_equal(res, expected)
337330

338331
def test_get_indexer_float64(self):
339-
idx = Float64Index([0.0, 1.0, 2.0])
332+
idx = Index([0.0, 1.0, 2.0], dtype=np.float64)
340333
tm.assert_numpy_array_equal(
341334
idx.get_indexer(idx), np.array([0, 1, 2], dtype=np.intp)
342335
)
@@ -354,39 +347,39 @@ def test_get_indexer_float64(self):
354347

355348
def test_get_indexer_nan(self):
356349
# GH#7820
357-
result = Float64Index([1, 2, np.nan]).get_indexer([np.nan])
350+
result = Index([1, 2, np.nan], dtype=np.float64).get_indexer([np.nan])
358351
expected = np.array([2], dtype=np.intp)
359352
tm.assert_numpy_array_equal(result, expected)
360353

361354
def test_get_indexer_int64(self):
362-
index = Int64Index(range(0, 20, 2))
363-
target = Int64Index(np.arange(10))
355+
index = Index(range(0, 20, 2), dtype=np.int64)
356+
target = Index(np.arange(10), dtype=np.int64)
364357
indexer = index.get_indexer(target)
365358
expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
366359
tm.assert_numpy_array_equal(indexer, expected)
367360

368-
target = Int64Index(np.arange(10))
361+
target = Index(np.arange(10), dtype=np.int64)
369362
indexer = index.get_indexer(target, method="pad")
370363
expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
371364
tm.assert_numpy_array_equal(indexer, expected)
372365

373-
target = Int64Index(np.arange(10))
366+
target = Index(np.arange(10), dtype=np.int64)
374367
indexer = index.get_indexer(target, method="backfill")
375368
expected = np.array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5], dtype=np.intp)
376369
tm.assert_numpy_array_equal(indexer, expected)
377370

378371
def test_get_indexer_uint64(self, index_large):
379-
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
372+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
380373
indexer = index_large.get_indexer(target)
381374
expected = np.array([0, -1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
382375
tm.assert_numpy_array_equal(indexer, expected)
383376

384-
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
377+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
385378
indexer = index_large.get_indexer(target, method="pad")
386379
expected = np.array([0, 0, 1, 2, 3, 4, 4, 4, 4, 4], dtype=np.intp)
387380
tm.assert_numpy_array_equal(indexer, expected)
388381

389-
target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
382+
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
390383
indexer = index_large.get_indexer(target, method="backfill")
391384
expected = np.array([0, 1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
392385
tm.assert_numpy_array_equal(indexer, expected)
@@ -396,9 +389,9 @@ class TestWhere:
396389
@pytest.mark.parametrize(
397390
"index",
398391
[
399-
Float64Index(np.arange(5, dtype="float64")),
400-
Int64Index(range(0, 20, 2)),
401-
UInt64Index(np.arange(5, dtype="uint64")),
392+
Index(np.arange(5, dtype="float64")),
393+
Index(range(0, 20, 2), dtype=np.int64),
394+
Index(np.arange(5, dtype="uint64")),
402395
],
403396
)
404397
def test_where(self, listlike_box, index):
@@ -407,16 +400,16 @@ def test_where(self, listlike_box, index):
407400
result = index.where(listlike_box(cond))
408401

409402
cond = [False] + [True] * (len(index) - 1)
410-
expected = Float64Index([index._na_value] + index[1:].tolist())
403+
expected = Index([index._na_value] + index[1:].tolist(), dtype=np.float64)
411404
result = index.where(listlike_box(cond))
412405
tm.assert_index_equal(result, expected)
413406

414407
def test_where_uint64(self):
415-
idx = UInt64Index([0, 6, 2])
408+
idx = Index([0, 6, 2], dtype=np.uint64)
416409
mask = np.array([False, True, False])
417410
other = np.array([1], dtype=np.int64)
418411

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

421414
result = idx.where(mask, other)
422415
tm.assert_index_equal(result, expected)
@@ -437,27 +430,27 @@ def test_where_infers_type_instead_of_trying_to_convert_string_to_float(self):
437430

438431

439432
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")
433+
@pytest.mark.parametrize("idx_dtype", [np.float64, np.int64, np.uint64])
434+
def test_take_preserve_name(self, idx_dtype):
435+
index = Index([1, 2, 3, 4], dtype=idx_dtype, name="foo")
443436
taken = index.take([3, 0, 1])
444437
assert index.name == taken.name
445438

446439
def test_take_fill_value_float64(self):
447440
# GH 12631
448-
idx = Float64Index([1.0, 2.0, 3.0], name="xxx")
441+
idx = Index([1.0, 2.0, 3.0], name="xxx", dtype=np.float64)
449442
result = idx.take(np.array([1, 0, -1]))
450-
expected = Float64Index([2.0, 1.0, 3.0], name="xxx")
443+
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
451444
tm.assert_index_equal(result, expected)
452445

453446
# fill_value
454447
result = idx.take(np.array([1, 0, -1]), fill_value=True)
455-
expected = Float64Index([2.0, 1.0, np.nan], name="xxx")
448+
expected = Index([2.0, 1.0, np.nan], dtype=np.float64, name="xxx")
456449
tm.assert_index_equal(result, expected)
457450

458451
# allow_fill=False
459452
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")
453+
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
461454
tm.assert_index_equal(result, expected)
462455

463456
msg = (
@@ -473,15 +466,15 @@ def test_take_fill_value_float64(self):
473466
with pytest.raises(IndexError, match=msg):
474467
idx.take(np.array([1, -5]))
475468

476-
@pytest.mark.parametrize("klass", [Int64Index, UInt64Index])
477-
def test_take_fill_value_ints(self, klass):
469+
@pytest.mark.parametrize("dtype", [np.int64, np.uint64])
470+
def test_take_fill_value_ints(self, dtype):
478471
# see gh-12631
479-
idx = klass([1, 2, 3], name="xxx")
472+
idx = Index([1, 2, 3], dtype=dtype, name="xxx")
480473
result = idx.take(np.array([1, 0, -1]))
481-
expected = klass([2, 1, 3], name="xxx")
474+
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
482475
tm.assert_index_equal(result, expected)
483476

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

487480
# fill_value=True
@@ -490,7 +483,7 @@ def test_take_fill_value_ints(self, klass):
490483

491484
# allow_fill=False
492485
result = idx.take(np.array([1, 0, -1]), allow_fill=False, fill_value=True)
493-
expected = klass([2, 1, 3], name="xxx")
486+
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
494487
tm.assert_index_equal(result, expected)
495488

496489
with pytest.raises(ValueError, match=msg):
@@ -504,18 +497,18 @@ def test_take_fill_value_ints(self, klass):
504497

505498

506499
class TestContains:
507-
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])
508-
def test_contains_none(self, klass):
500+
@pytest.mark.parametrize("dtype", [np.float64, np.int64, np.uint64])
501+
def test_contains_none(self, dtype):
509502
# GH#35788 should return False, not raise TypeError
510-
index = klass([0, 1, 2, 3, 4])
503+
index = Index([0, 1, 2, 3, 4], dtype=dtype)
511504
assert None not in index
512505

513506
def test_contains_float64_nans(self):
514-
index = Float64Index([1.0, 2.0, np.nan])
507+
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
515508
assert np.nan in index
516509

517510
def test_contains_float64_not_nans(self):
518-
index = Float64Index([1.0, 2.0, np.nan])
511+
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
519512
assert 1.0 in index
520513

521514

0 commit comments

Comments
 (0)