Skip to content

DEPR: remove Int64Index, UInt64Index, Float64Index from tests.indexes.numeric #49682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,19 @@ def test_can_hold_identifiers(self, simple_index):
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is False

def test_view(self, dtype):
index_cls = self._index_cls

idx = index_cls([], dtype=dtype, name="Foo")
idx_view = idx.view()
assert idx_view.name == "Foo"

idx_view = idx.view(dtype)
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)

idx_view = idx.view(index_cls)
tm.assert_index_equal(idx, index_cls(idx_view, name="Foo"), exact=True)

def test_format(self, simple_index):
# GH35439
idx = simple_index
Expand Down
45 changes: 20 additions & 25 deletions pandas/tests/indexes/numeric/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,67 @@

from pandas import Index
import pandas._testing as tm
from pandas.core.indexes.api import (
Float64Index,
Int64Index,
UInt64Index,
)


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

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

def test_astype_float64_to_object(self):
float_index = Float64Index([0.0, 2.5, 5.0, 7.5, 10.0])
float_index = Index([0.0, 2.5, 5.0, 7.5, 10.0], dtype=np.float64)
result = float_index.astype(object)
assert result.equals(float_index)
assert float_index.equals(result)
assert isinstance(result, Index) and not isinstance(result, Float64Index)
assert isinstance(result, Index) and result.dtype == object

def test_astype_float64_mixed_to_object(self):
# mixed int-float
idx = Float64Index([1.5, 2, 3, 4, 5])
idx = Index([1.5, 2, 3, 4, 5], dtype=np.float64)
idx.name = "foo"
result = idx.astype(object)
assert result.equals(idx)
assert idx.equals(result)
assert isinstance(result, Index) and not isinstance(result, Float64Index)
assert isinstance(result, Index) and result.dtype == object

@pytest.mark.parametrize("dtype", ["int16", "int32", "int64"])
def test_astype_float64_to_int_dtype(self, dtype):
# GH#12881
# a float astype int
idx = Float64Index([0, 1, 2])
idx = Index([0, 1, 2], dtype=np.float64)
result = idx.astype(dtype)
expected = Int64Index([0, 1, 2])
tm.assert_index_equal(result, expected)
expected = Index([0, 1, 2], dtype=np.int64)
tm.assert_index_equal(result, expected, exact=True)

idx = Float64Index([0, 1.1, 2])
idx = Index([0, 1.1, 2], dtype=np.float64)
result = idx.astype(dtype)
expected = Int64Index([0, 1, 2])
tm.assert_index_equal(result, expected)
expected = Index([0, 1, 2], dtype=dtype)
tm.assert_index_equal(result, expected, exact=True)

@pytest.mark.parametrize("dtype", ["float32", "float64"])
def test_astype_float64_to_float_dtype(self, dtype):
# GH#12881
# a float astype int
idx = Float64Index([0, 1, 2])
idx = Index([0, 1, 2], dtype=np.float64)
result = idx.astype(dtype)
expected = idx
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact=True)

idx = Float64Index([0, 1.1, 2])
idx = Index([0, 1.1, 2], dtype=np.float64)
result = idx.astype(dtype)
expected = Index(idx.values.astype(dtype))
tm.assert_index_equal(result, expected)
tm.assert_index_equal(result, expected, exact=True)

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

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

msg = r"Cannot convert non-finite values \(NA or inf\) to integer"
with pytest.raises(ValueError, match=msg):
Expand All @@ -94,6 +89,6 @@ def test_cannot_cast_inf_to_int(self, non_finite, dtype):
def test_astype_from_object(self):
index = Index([1.0, np.nan, 0.2], dtype="object")
result = index.astype(float)
expected = Float64Index([1.0, np.nan, 0.2])
expected = Index([1.0, np.nan, 0.2], dtype=np.float64)
assert result.dtype == expected.dtype
tm.assert_index_equal(result, expected)
97 changes: 47 additions & 50 deletions pandas/tests/indexes/numeric/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@
Timestamp,
)
import pandas._testing as tm
from pandas.core.indexes.api import (
Float64Index,
Int64Index,
UInt64Index,
)


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


class TestGetLoc:
Expand Down Expand Up @@ -86,7 +81,7 @@ def test_get_loc_raises_missized_tolerance(self):

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

def test_get_loc_na(self):
idx = Float64Index([np.nan, 1, 2])
idx = Index([np.nan, 1, 2], dtype=np.float64)
assert idx.get_loc(1) == 1
assert idx.get_loc(np.nan) == 0

idx = Float64Index([np.nan, 1, np.nan])
idx = Index([np.nan, 1, np.nan], dtype=np.float64)
assert idx.get_loc(1) == 1

# representable by slice [0:2:2]
msg = "'Cannot get left slice bound for non-unique label: nan'"
with pytest.raises(KeyError, match=msg):
idx.slice_locs(np.nan)
# not representable by slice
idx = Float64Index([np.nan, 1, np.nan, np.nan])
idx = Index([np.nan, 1, np.nan, np.nan], dtype=np.float64)
assert idx.get_loc(1) == 1
msg = "'Cannot get left slice bound for non-unique label: nan"
with pytest.raises(KeyError, match=msg):
idx.slice_locs(np.nan)

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

@pytest.mark.parametrize(
"idx_class", [Int64Index, RangeIndex, Float64Index, UInt64Index]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do the RangeIndex cases need to be added back somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I'll work something into this take this into account.

)
@pytest.mark.parametrize("idx_dtype", ["int64", "float64", "uint64", "range"])
@pytest.mark.parametrize("method", ["get_indexer", "get_indexer_non_unique"])
def test_get_indexer_numeric_index_boolean_target(self, method, idx_class):
def test_get_indexer_numeric_index_boolean_target(self, method, idx_dtype):
# GH 16877

numeric_index = idx_class(RangeIndex(4))
if idx_dtype == "range":
numeric_index = RangeIndex(4)
else:
numeric_index = Index(np.arange(4, dtype=idx_dtype))

other = Index([True, False, True])

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

def test_get_indexer_float64(self):
idx = Float64Index([0.0, 1.0, 2.0])
idx = Index([0.0, 1.0, 2.0], dtype=np.float64)
tm.assert_numpy_array_equal(
idx.get_indexer(idx), np.array([0, 1, 2], dtype=np.intp)
)
Expand All @@ -354,39 +351,39 @@ def test_get_indexer_float64(self):

def test_get_indexer_nan(self):
# GH#7820
result = Float64Index([1, 2, np.nan]).get_indexer([np.nan])
result = Index([1, 2, np.nan], dtype=np.float64).get_indexer([np.nan])
expected = np.array([2], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

def test_get_indexer_int64(self):
index = Int64Index(range(0, 20, 2))
target = Int64Index(np.arange(10))
index = Index(range(0, 20, 2), dtype=np.int64)
target = Index(np.arange(10), dtype=np.int64)
indexer = index.get_indexer(target)
expected = np.array([0, -1, 1, -1, 2, -1, 3, -1, 4, -1], dtype=np.intp)
tm.assert_numpy_array_equal(indexer, expected)

target = Int64Index(np.arange(10))
target = Index(np.arange(10), dtype=np.int64)
indexer = index.get_indexer(target, method="pad")
expected = np.array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], dtype=np.intp)
tm.assert_numpy_array_equal(indexer, expected)

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

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

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

target = UInt64Index(np.arange(10).astype("uint64") * 5 + 2**63)
target = Index(np.arange(10).astype("uint64") * 5 + 2**63)
indexer = index_large.get_indexer(target, method="backfill")
expected = np.array([0, 1, 1, 2, 3, 4, -1, -1, -1, -1], dtype=np.intp)
tm.assert_numpy_array_equal(indexer, expected)
Expand All @@ -396,9 +393,9 @@ class TestWhere:
@pytest.mark.parametrize(
"index",
[
Float64Index(np.arange(5, dtype="float64")),
Int64Index(range(0, 20, 2)),
UInt64Index(np.arange(5, dtype="uint64")),
Index(np.arange(5, dtype="float64")),
Index(range(0, 20, 2), dtype=np.int64),
Index(np.arange(5, dtype="uint64")),
],
)
def test_where(self, listlike_box, index):
Expand All @@ -407,16 +404,16 @@ def test_where(self, listlike_box, index):
result = index.where(listlike_box(cond))

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

def test_where_uint64(self):
idx = UInt64Index([0, 6, 2])
idx = Index([0, 6, 2], dtype=np.uint64)
mask = np.array([False, True, False])
other = np.array([1], dtype=np.int64)

expected = UInt64Index([1, 6, 1])
expected = Index([1, 6, 1], dtype=np.uint64)

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


class TestTake:
@pytest.mark.parametrize("klass", [Float64Index, Int64Index, UInt64Index])
def test_take_preserve_name(self, klass):
index = klass([1, 2, 3, 4], name="foo")
@pytest.mark.parametrize("idx_dtype", [np.float64, np.int64, np.uint64])
def test_take_preserve_name(self, idx_dtype):
index = Index([1, 2, 3, 4], dtype=idx_dtype, name="foo")
taken = index.take([3, 0, 1])
assert index.name == taken.name

def test_take_fill_value_float64(self):
# GH 12631
idx = Float64Index([1.0, 2.0, 3.0], name="xxx")
idx = Index([1.0, 2.0, 3.0], name="xxx", dtype=np.float64)
result = idx.take(np.array([1, 0, -1]))
expected = Float64Index([2.0, 1.0, 3.0], name="xxx")
expected = Index([2.0, 1.0, 3.0], dtype=np.float64, name="xxx")
tm.assert_index_equal(result, expected)

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

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

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

@pytest.mark.parametrize("klass", [Int64Index, UInt64Index])
def test_take_fill_value_ints(self, klass):
@pytest.mark.parametrize("dtype", [np.int64, np.uint64])
def test_take_fill_value_ints(self, dtype):
# see gh-12631
idx = klass([1, 2, 3], name="xxx")
idx = Index([1, 2, 3], dtype=dtype, name="xxx")
result = idx.take(np.array([1, 0, -1]))
expected = klass([2, 1, 3], name="xxx")
expected = Index([2, 1, 3], dtype=dtype, name="xxx")
tm.assert_index_equal(result, expected)

name = klass.__name__
name = type(idx).__name__
msg = f"Unable to fill values because {name} cannot contain NA"

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

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

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


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

def test_contains_float64_nans(self):
index = Float64Index([1.0, 2.0, np.nan])
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
assert np.nan in index

def test_contains_float64_not_nans(self):
index = Float64Index([1.0, 2.0, np.nan])
index = Index([1.0, 2.0, np.nan], dtype=np.float64)
assert 1.0 in index


Expand Down
Loading