Skip to content

Commit 6a40a6c

Browse files
topper-123pooja-subramaniam
authored andcommitted
DEPR: remove Int/Uint/Float64Index from pandas/tests/indexes/ranges (pandas-dev#50826)
* remove Int/Uint/Float64Index from pandas/tests/indexes/ranges * remove Int64Index * is_int64_dtype * pre-commit
1 parent 79f23d0 commit 6a40a6c

File tree

4 files changed

+85
-93
lines changed

4 files changed

+85
-93
lines changed

pandas/core/indexes/range.py

+21-27
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@
4747
from pandas.core.construction import extract_array
4848
import pandas.core.indexes.base as ibase
4949
from pandas.core.indexes.base import maybe_extract_name
50-
from pandas.core.indexes.numeric import (
51-
Float64Index,
52-
Int64Index,
53-
NumericIndex,
54-
)
50+
from pandas.core.indexes.numeric import NumericIndex
5551
from pandas.core.ops.common import unpack_zerodim_and_defer
5652

5753
if TYPE_CHECKING:
@@ -64,8 +60,8 @@ class RangeIndex(NumericIndex):
6460
"""
6561
Immutable Index implementing a monotonic integer range.
6662
67-
RangeIndex is a memory-saving special case of Int64Index limited to
68-
representing monotonic ranges. Using RangeIndex may in some instances
63+
RangeIndex is a memory-saving special case of an Index limited to representing
64+
monotonic ranges with a 64-bit dtype. Using RangeIndex may in some instances
6965
improve computing speed.
7066
7167
This is the default index type used
@@ -97,7 +93,6 @@ class RangeIndex(NumericIndex):
9793
See Also
9894
--------
9995
Index : The base pandas Index type.
100-
Int64Index : Index of int64 data.
10196
"""
10297

10398
_typ = "rangeindex"
@@ -185,7 +180,7 @@ def _simple_new( # type: ignore[override]
185180

186181
# --------------------------------------------------------------------
187182

188-
# error: Return type "Type[Int64Index]" of "_constructor" incompatible with return
183+
# error: Return type "Type[NumericIndex]" of "_constructor" incompatible with return
189184
# type "Type[RangeIndex]" in supertype "Index"
190185
@cache_readonly
191186
def _constructor(self) -> type[NumericIndex]: # type: ignore[override]
@@ -331,7 +326,7 @@ def inferred_type(self) -> str:
331326
# --------------------------------------------------------------------
332327
# Indexing Methods
333328

334-
@doc(Int64Index.get_loc)
329+
@doc(NumericIndex.get_loc)
335330
def get_loc(self, key):
336331
if is_integer(key) or (is_float(key) and key.is_integer()):
337332
new_key = int(key)
@@ -377,32 +372,32 @@ def _get_indexer(
377372
def tolist(self) -> list[int]:
378373
return list(self._range)
379374

380-
@doc(Int64Index.__iter__)
375+
@doc(NumericIndex.__iter__)
381376
def __iter__(self) -> Iterator[int]:
382377
yield from self._range
383378

384-
@doc(Int64Index._shallow_copy)
379+
@doc(NumericIndex._shallow_copy)
385380
def _shallow_copy(self, values, name: Hashable = no_default):
386381
name = self.name if name is no_default else name
387382

388383
if values.dtype.kind == "f":
389-
return Float64Index(values, name=name)
384+
return NumericIndex(values, name=name, dtype=np.float64)
390385
# GH 46675 & 43885: If values is equally spaced, return a
391-
# more memory-compact RangeIndex instead of Int64Index
386+
# more memory-compact RangeIndex instead of Index with 64-bit dtype
392387
unique_diffs = unique_deltas(values)
393388
if len(unique_diffs) == 1 and unique_diffs[0] != 0:
394389
diff = unique_diffs[0]
395390
new_range = range(values[0], values[-1] + diff, diff)
396391
return type(self)._simple_new(new_range, name=name)
397392
else:
398-
return Int64Index._simple_new(values, name=name)
393+
return NumericIndex._simple_new(values, name=name)
399394

400395
def _view(self: RangeIndex) -> RangeIndex:
401396
result = type(self)._simple_new(self._range, name=self._name)
402397
result._cache = self._cache
403398
return result
404399

405-
@doc(Int64Index.copy)
400+
@doc(NumericIndex.copy)
406401
def copy(self, name: Hashable = None, deep: bool = False):
407402
name = self._validate_names(name=name, deep=deep)[0]
408403
new_index = self._rename(name=name)
@@ -517,7 +512,6 @@ def _intersection(self, other: Index, sort: bool = False):
517512
# caller is responsible for checking self and other are both non-empty
518513

519514
if not isinstance(other, RangeIndex):
520-
# Int64Index
521515
return super()._intersection(other, sort=sort)
522516

523517
first = self._range[::-1] if self.step < 0 else self._range
@@ -604,10 +598,10 @@ def _union(self, other: Index, sort):
604598
sort : False or None, default None
605599
Whether to sort (monotonically increasing) the resulting index.
606600
``sort=None`` returns a ``RangeIndex`` if possible or a sorted
607-
``Int64Index`` if not.
601+
``Index`` with a int64 dtype if not.
608602
``sort=False`` can return a ``RangeIndex`` if self is monotonically
609603
increasing and other is fully contained in self. Otherwise, returns
610-
an unsorted ``Int64Index``
604+
an unsorted ``Index`` with an int64 dtype.
611605
612606
Returns
613607
-------
@@ -819,9 +813,9 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
819813
Overriding parent method for the case of all RangeIndex instances.
820814
821815
When all members of "indexes" are of type RangeIndex: result will be
822-
RangeIndex if possible, Int64Index otherwise. E.g.:
816+
RangeIndex if possible, Index with a int64 dtype otherwise. E.g.:
823817
indexes = [RangeIndex(3), RangeIndex(3, 6)] -> RangeIndex(6)
824-
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Int64Index([0,1,2,4,5])
818+
indexes = [RangeIndex(3), RangeIndex(4, 6)] -> Index([0,1,2,4,5], dtype='int64')
825819
"""
826820
if not all(isinstance(x, RangeIndex) for x in indexes):
827821
return super()._concat(indexes, name)
@@ -848,7 +842,7 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
848842
# First non-empty index had only one element
849843
if rng.start == start:
850844
values = np.concatenate([x._values for x in rng_indexes])
851-
result = Int64Index(values)
845+
result = self._constructor(values)
852846
return result.rename(name)
853847

854848
step = rng.start - start
@@ -857,7 +851,9 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
857851
next_ is not None and rng.start != next_
858852
)
859853
if non_consecutive:
860-
result = Int64Index(np.concatenate([x._values for x in rng_indexes]))
854+
result = self._constructor(
855+
np.concatenate([x._values for x in rng_indexes])
856+
)
861857
return result.rename(name)
862858

863859
if step is not None:
@@ -905,7 +901,6 @@ def __getitem__(self, key):
905901
"and integer or boolean "
906902
"arrays are valid indices"
907903
)
908-
# fall back to Int64Index
909904
return super().__getitem__(key)
910905

911906
def _getitem_slice(self: RangeIndex, slobj: slice) -> RangeIndex:
@@ -1010,15 +1005,14 @@ def _arith_method(self, other, op):
10101005
res_name = ops.get_op_result_name(self, other)
10111006
result = type(self)(rstart, rstop, rstep, name=res_name)
10121007

1013-
# for compat with numpy / Int64Index
1008+
# for compat with numpy / Index with int64 dtype
10141009
# even if we can represent as a RangeIndex, return
1015-
# as a Float64Index if we have float-like descriptors
1010+
# as a float64 Index if we have float-like descriptors
10161011
if not all(is_integer(x) for x in [rstart, rstop, rstep]):
10171012
result = result.astype("float64")
10181013

10191014
return result
10201015

10211016
except (ValueError, TypeError, ZeroDivisionError):
1022-
# Defer to Int64Index implementation
10231017
# test_arithmetic_explicit_conversions
10241018
return super()._arith_method(other, op)

pandas/tests/indexes/ranges/test_join.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
import numpy as np
22

3+
from pandas.core.dtypes.common import is_int64_dtype
4+
35
from pandas import (
46
Index,
57
RangeIndex,
68
)
79
import pandas._testing as tm
8-
from pandas.core.indexes.api import Int64Index
910

1011

1112
class TestJoin:
1213
def test_join_outer(self):
13-
# join with Int64Index
14+
# join with Index[int64]
1415
index = RangeIndex(start=0, stop=20, step=2)
15-
other = Int64Index(np.arange(25, 14, -1))
16+
other = Index(np.arange(25, 14, -1, dtype=np.int64))
1617

1718
res, lidx, ridx = index.join(other, how="outer", return_indexers=True)
1819
noidx_res = index.join(other, how="outer")
1920
tm.assert_index_equal(res, noidx_res)
2021

21-
eres = Int64Index(
22+
eres = Index(
2223
[0, 2, 4, 6, 8, 10, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
2324
)
2425
elidx = np.array(
@@ -30,9 +31,9 @@ def test_join_outer(self):
3031
dtype=np.intp,
3132
)
3233

33-
assert isinstance(res, Int64Index)
34+
assert isinstance(res, Index) and is_int64_dtype(res.dtype)
3435
assert not isinstance(res, RangeIndex)
35-
tm.assert_index_equal(res, eres)
36+
tm.assert_index_equal(res, eres, exact=True)
3637
tm.assert_numpy_array_equal(lidx, elidx)
3738
tm.assert_numpy_array_equal(ridx, eridx)
3839

@@ -43,7 +44,7 @@ def test_join_outer(self):
4344
noidx_res = index.join(other, how="outer")
4445
tm.assert_index_equal(res, noidx_res)
4546

46-
assert isinstance(res, Int64Index)
47+
assert isinstance(res, Index) and res.dtype == np.int64
4748
assert not isinstance(res, RangeIndex)
4849
tm.assert_index_equal(res, eres)
4950
tm.assert_numpy_array_equal(lidx, elidx)
@@ -52,7 +53,7 @@ def test_join_outer(self):
5253
def test_join_inner(self):
5354
# Join with non-RangeIndex
5455
index = RangeIndex(start=0, stop=20, step=2)
55-
other = Int64Index(np.arange(25, 14, -1))
56+
other = Index(np.arange(25, 14, -1, dtype=np.int64))
5657

5758
res, lidx, ridx = index.join(other, how="inner", return_indexers=True)
5859

@@ -62,7 +63,7 @@ def test_join_inner(self):
6263
lidx = lidx.take(ind)
6364
ridx = ridx.take(ind)
6465

65-
eres = Int64Index([16, 18])
66+
eres = Index([16, 18])
6667
elidx = np.array([8, 9], dtype=np.intp)
6768
eridx = np.array([9, 7], dtype=np.intp)
6869

@@ -82,9 +83,9 @@ def test_join_inner(self):
8283
tm.assert_numpy_array_equal(ridx, eridx)
8384

8485
def test_join_left(self):
85-
# Join with Int64Index
86+
# Join with Index[int64]
8687
index = RangeIndex(start=0, stop=20, step=2)
87-
other = Int64Index(np.arange(25, 14, -1))
88+
other = Index(np.arange(25, 14, -1, dtype=np.int64))
8889

8990
res, lidx, ridx = index.join(other, how="left", return_indexers=True)
9091
eres = index
@@ -96,7 +97,7 @@ def test_join_left(self):
9697
tm.assert_numpy_array_equal(ridx, eridx)
9798

9899
# Join withRangeIndex
99-
other = Int64Index(np.arange(25, 14, -1))
100+
other = Index(np.arange(25, 14, -1, dtype=np.int64))
100101

101102
res, lidx, ridx = index.join(other, how="left", return_indexers=True)
102103

@@ -106,15 +107,15 @@ def test_join_left(self):
106107
tm.assert_numpy_array_equal(ridx, eridx)
107108

108109
def test_join_right(self):
109-
# Join with Int64Index
110+
# Join with Index[int64]
110111
index = RangeIndex(start=0, stop=20, step=2)
111-
other = Int64Index(np.arange(25, 14, -1))
112+
other = Index(np.arange(25, 14, -1, dtype=np.int64))
112113

113114
res, lidx, ridx = index.join(other, how="right", return_indexers=True)
114115
eres = other
115116
elidx = np.array([-1, -1, -1, -1, -1, -1, -1, 9, -1, 8, -1], dtype=np.intp)
116117

117-
assert isinstance(other, Int64Index)
118+
assert isinstance(other, Index) and other.dtype == np.int64
118119
tm.assert_index_equal(res, eres)
119120
tm.assert_numpy_array_equal(lidx, elidx)
120121
assert ridx is None
@@ -164,7 +165,7 @@ def test_join_non_unique(self):
164165

165166
res, lidx, ridx = index.join(other, return_indexers=True)
166167

167-
eres = Int64Index([0, 2, 4, 4, 6, 8, 10, 12, 14, 16, 18])
168+
eres = Index([0, 2, 4, 4, 6, 8, 10, 12, 14, 16, 18])
168169
elidx = np.array([0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.intp)
169170
eridx = np.array([-1, -1, 0, 1, -1, -1, -1, -1, -1, -1, -1], dtype=np.intp)
170171

pandas/tests/indexes/ranges/test_range.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
from pandas.core.dtypes.common import ensure_platform_int
55

66
import pandas as pd
7-
import pandas._testing as tm
8-
from pandas.core.indexes.api import (
9-
Float64Index,
7+
from pandas import (
108
Index,
11-
Int64Index,
129
RangeIndex,
1310
)
11+
import pandas._testing as tm
1412
from pandas.tests.indexes.common import NumericBase
1513

1614
# aliases to make some tests easier to read
1715
RI = RangeIndex
18-
I64 = Int64Index
19-
F64 = Float64Index
20-
OI = Index
2116

2217

2318
class TestRangeIndex(NumericBase):
@@ -111,7 +106,7 @@ def test_insert(self):
111106
tm.assert_index_equal(idx[0:4], result.insert(0, idx[0]), exact="equiv")
112107

113108
# GH 18295 (test missing)
114-
expected = Float64Index([0, np.nan, 1, 2, 3, 4])
109+
expected = Index([0, np.nan, 1, 2, 3, 4], dtype=np.float64)
115110
for na in [np.nan, None, pd.NA]:
116111
result = RangeIndex(5).insert(1, na)
117112
tm.assert_index_equal(result, expected)
@@ -379,7 +374,7 @@ def test_nbytes(self):
379374

380375
# memory savings vs int index
381376
idx = RangeIndex(0, 1000)
382-
assert idx.nbytes < Int64Index(idx._values).nbytes / 10
377+
assert idx.nbytes < Index(idx._values).nbytes / 10
383378

384379
# constant memory usage
385380
i2 = RangeIndex(0, 10)
@@ -530,16 +525,16 @@ def test_len_specialised(self, step):
530525
([RI(-4, -8), RI(-8, -12)], RI(0, 0)),
531526
([RI(-4, -8), RI(3, -4)], RI(0, 0)),
532527
([RI(-4, -8), RI(3, 5)], RI(3, 5)),
533-
([RI(-4, -2), RI(3, 5)], I64([-4, -3, 3, 4])),
528+
([RI(-4, -2), RI(3, 5)], Index([-4, -3, 3, 4])),
534529
([RI(-2), RI(3, 5)], RI(3, 5)),
535-
([RI(2), RI(2)], I64([0, 1, 0, 1])),
530+
([RI(2), RI(2)], Index([0, 1, 0, 1])),
536531
([RI(2), RI(2, 5), RI(5, 8, 4)], RI(0, 6)),
537-
([RI(2), RI(3, 5), RI(5, 8, 4)], I64([0, 1, 3, 4, 5])),
532+
([RI(2), RI(3, 5), RI(5, 8, 4)], Index([0, 1, 3, 4, 5])),
538533
([RI(-2, 2), RI(2, 5), RI(5, 8, 4)], RI(-2, 6)),
539-
([RI(3), OI([-1, 3, 15])], OI([0, 1, 2, -1, 3, 15])),
540-
([RI(3), OI([-1, 3.1, 15.0])], OI([0, 1, 2, -1, 3.1, 15.0])),
541-
([RI(3), OI(["a", None, 14])], OI([0, 1, 2, "a", None, 14])),
542-
([RI(3, 1), OI(["a", None, 14])], OI(["a", None, 14])),
534+
([RI(3), Index([-1, 3, 15])], Index([0, 1, 2, -1, 3, 15])),
535+
([RI(3), Index([-1, 3.1, 15.0])], Index([0, 1, 2, -1, 3.1, 15.0])),
536+
([RI(3), Index(["a", None, 14])], Index([0, 1, 2, "a", None, 14])),
537+
([RI(3, 1), Index(["a", None, 14])], Index(["a", None, 14])),
543538
]
544539
)
545540
def appends(self, request):

0 commit comments

Comments
 (0)