Skip to content
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

DEPR: Remove int64 uint64 float64 index tests #50479

50 changes: 1 addition & 49 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
ensure_platform_int,
is_bool_dtype,
is_categorical_dtype,
is_complex_dtype,
is_dtype_equal,
is_ea_or_datetimelike_dtype,
is_extension_array_dtype,
Expand All @@ -107,7 +106,6 @@
is_scalar,
is_signed_integer_dtype,
is_string_dtype,
is_unsigned_integer_dtype,
needs_i8_conversion,
pandas_dtype,
validate_all_hashable,
Expand All @@ -125,7 +123,6 @@
ABCDatetimeIndex,
ABCMultiIndex,
ABCPeriodIndex,
ABCRangeIndex,
ABCSeries,
ABCTimedeltaIndex,
)
Expand Down Expand Up @@ -395,7 +392,6 @@ def _outer_indexer(
# Whether this index is a NumericIndex, but not a Int64Index, Float64Index,
# UInt64Index or RangeIndex. Needed for backwards compat. Remove this attribute and
# associated code in pandas 2.0.
_is_backward_compat_public_numeric_index: bool = False

@property
def _engine_type(
Expand Down Expand Up @@ -446,13 +442,6 @@ def __new__(
elif is_ea_or_datetimelike_dtype(data_dtype):
pass

# index-like
elif (
isinstance(data, Index)
and data._is_backward_compat_public_numeric_index
and dtype is None
):
return data._constructor(data, name=name, copy=copy)
elif isinstance(data, (np.ndarray, Index, ABCSeries)):

if isinstance(data, ABCMultiIndex):
Expand Down Expand Up @@ -981,34 +970,6 @@ def astype(self, dtype, copy: bool = True):
new_values = astype_array(values, dtype=dtype, copy=copy)

# pass copy=False because any copying will be done in the astype above
if not self._is_backward_compat_public_numeric_index and not isinstance(
self, ABCRangeIndex
):
# this block is needed so e.g. Int64Index.astype("int32") returns
# Int64Index and not a NumericIndex with dtype int32.
# When Int64Index etc. are removed from the code base, removed this also.
if (
isinstance(dtype, np.dtype)
and is_numeric_dtype(dtype)
and not is_complex_dtype(dtype)
):
from pandas.core.api import (
Float64Index,
Int64Index,
UInt64Index,
)

klass: type[Index]
if is_signed_integer_dtype(dtype):
klass = Int64Index
elif is_unsigned_integer_dtype(dtype):
klass = UInt64Index
elif is_float_dtype(dtype):
klass = Float64Index
else:
klass = Index
return klass(new_values, name=self.name, dtype=dtype, copy=False)

return Index(new_values, name=self.name, dtype=new_values.dtype, copy=False)

_index_shared_docs[
Expand Down Expand Up @@ -5075,10 +5036,6 @@ def _concat(self, to_concat: list[Index], name: Hashable) -> Index:

result = concat_compat(to_concat_vals)

is_numeric = result.dtype.kind in ["i", "u", "f"]
if self._is_backward_compat_public_numeric_index and is_numeric:
return type(self)._simple_new(result, name=name)

return Index._with_infer(result, name=name)

def putmask(self, mask, value) -> Index:
Expand Down Expand Up @@ -6476,12 +6433,7 @@ def insert(self, loc: int, item) -> Index:
loc = loc if loc >= 0 else loc - 1
new_values[loc] = item

if self._typ == "numericindex":
# Use self._constructor instead of Index to retain NumericIndex GH#43921
# TODO(2.0) can use Index instead of self._constructor
return self._constructor(new_values, name=self.name)
else:
return Index._with_infer(new_values, name=self.name)
return Index._with_infer(new_values, name=self.name)

def drop(
self,
Expand Down
25 changes: 0 additions & 25 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from pandas.core.dtypes.common import (
is_categorical_dtype,
is_scalar,
pandas_dtype,
)
from pandas.core.dtypes.missing import (
is_valid_na_for_dtype,
Expand Down Expand Up @@ -274,30 +273,6 @@ def _is_dtype_compat(self, other) -> Categorical:

return other

@doc(Index.astype)
def astype(self, dtype: Dtype, copy: bool = True) -> Index:
from pandas.core.api import NumericIndex

dtype = pandas_dtype(dtype)

categories = self.categories
# the super method always returns Int64Index, UInt64Index and Float64Index
# but if the categories are a NumericIndex with dtype float32, we want to
# return an index with the same dtype as self.categories.
if categories._is_backward_compat_public_numeric_index:
assert isinstance(categories, NumericIndex) # mypy complaint fix
try:
categories._validate_dtype(dtype)
except ValueError:
pass
else:
new_values = self._data.astype(dtype, copy=copy)
# pass copy=False because any copying has been done in the
# _data.astype call above
return categories._constructor(new_values, name=self.name, copy=False)

return super().astype(dtype, copy=copy)

def equals(self, other: object) -> bool:
"""
Determine if two CategoricalIndex objects contain the same elements.
Expand Down
9 changes: 1 addition & 8 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class NumericIndex(Index):
"numeric type",
)
_can_hold_strings = False
_is_backward_compat_public_numeric_index: bool = True

_engine_types: dict[np.dtype, type[libindex.IndexEngine]] = {
np.dtype(np.int8): libindex.Int8Engine,
Expand Down Expand Up @@ -214,12 +213,7 @@ def _ensure_dtype(cls, dtype: Dtype | None) -> np.dtype | None:
# float16 not supported (no indexing engine)
raise NotImplementedError("float16 indexes are not supported")

if cls._is_backward_compat_public_numeric_index:
# dtype for NumericIndex
return dtype
else:
# dtype for Int64Index, UInt64Index etc. Needed for backwards compat.
return cls._default_dtype
return dtype

# ----------------------------------------------------------------
# Indexing Methods
Expand Down Expand Up @@ -415,7 +409,6 @@ class Float64Index(NumericIndex):
_typ = "float64index"
_default_dtype = np.dtype(np.float64)
_dtype_validation_metadata = (is_float_dtype, "float")
_is_backward_compat_public_numeric_index: bool = False

@property
def _engine_type(self) -> type[libindex.Float64Engine]:
Expand Down
13 changes: 6 additions & 7 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class RangeIndex(NumericIndex):
_typ = "rangeindex"
_dtype_validation_metadata = (is_signed_integer_dtype, "signed integer")
_range: range
_is_backward_compat_public_numeric_index: bool = False

@property
def _engine_type(self) -> type[libindex.Int64Engine]:
Expand Down Expand Up @@ -849,7 +848,7 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
# First non-empty index had only one element
if rng.start == start:
values = np.concatenate([x._values for x in rng_indexes])
result = Int64Index(values)
result = self._constructor(values)
return result.rename(name)

step = rng.start - start
Expand All @@ -858,7 +857,9 @@ def _concat(self, indexes: list[Index], name: Hashable) -> Index:
next_ is not None and rng.start != next_
)
if non_consecutive:
result = Int64Index(np.concatenate([x._values for x in rng_indexes]))
result = self._constructor(
np.concatenate([x._values for x in rng_indexes])
)
return result.rename(name)

if step is not None:
Expand Down Expand Up @@ -906,7 +907,6 @@ def __getitem__(self, key):
"and integer or boolean "
"arrays are valid indices"
)
# fall back to Int64Index
return super().__getitem__(key)

def _getitem_slice(self: RangeIndex, slobj: slice) -> RangeIndex:
Expand Down Expand Up @@ -1011,15 +1011,14 @@ def _arith_method(self, other, op):
res_name = ops.get_op_result_name(self, other)
result = type(self)(rstart, rstop, rstep, name=res_name)

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

return result

except (ValueError, TypeError, ZeroDivisionError):
# Defer to Int64Index implementation
# test_arithmetic_explicit_conversions
return super()._arith_method(other, op)
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
from pandas.core.indexes.accessors import CombinedDatetimelikeProperties
from pandas.core.indexes.api import (
DatetimeIndex,
Float64Index,
Index,
MultiIndex,
PeriodIndex,
Expand Down Expand Up @@ -2569,7 +2568,8 @@ def quantile(

if is_list_like(q):
result.name = self.name
return self._constructor(result, index=Float64Index(q), name=self.name)
idx = Index(q, dtype=np.float64)
return self._constructor(result, index=idx, name=self.name)
else:
# scalar
return result.iloc[0]
Expand Down
13 changes: 7 additions & 6 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
is_datetime64_dtype,
is_datetime64tz_dtype,
is_extension_array_dtype,
is_integer_dtype,
is_list_like,
is_string_dtype,
is_timedelta64_dtype,
Expand All @@ -88,7 +89,7 @@
concat,
isna,
)
from pandas.core.api import Int64Index
from pandas.core.api import NumericIndex
from pandas.core.arrays import (
Categorical,
DatetimeArray,
Expand Down Expand Up @@ -2240,13 +2241,13 @@ class GenericIndexCol(IndexCol):
def is_indexed(self) -> bool:
return False

# error: Return type "Tuple[Int64Index, Int64Index]" of "convert"
# error: Return type "Tuple[NumericIndex, NumericIndex]" of "convert"
# incompatible with return type "Union[Tuple[ndarray[Any, Any],
# ndarray[Any, Any]], Tuple[DatetimeIndex, DatetimeIndex]]" in
# supertype "IndexCol"
def convert( # type: ignore[override]
self, values: np.ndarray, nan_rep, encoding: str, errors: str
) -> tuple[Int64Index, Int64Index]:
) -> tuple[NumericIndex, NumericIndex]:
"""
Convert the data from this selection to the appropriate pandas type.

Expand All @@ -2259,7 +2260,7 @@ def convert( # type: ignore[override]
"""
assert isinstance(values, np.ndarray), type(values)

index = Int64Index(np.arange(len(values)))
index = NumericIndex(np.arange(len(values)), dtype=np.int64)
return index, index

def set_attr(self) -> None:
Expand Down Expand Up @@ -4862,11 +4863,11 @@ def _convert_index(name: str, index: Index, encoding: str, errors: str) -> Index
atom = DataIndexableCol._get_atom(converted)

if (
isinstance(index, Int64Index)
(isinstance(index, NumericIndex) and is_integer_dtype(index))
or needs_i8_conversion(index.dtype)
or is_bool_dtype(index.dtype)
):
# Includes Int64Index, RangeIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex,
# Includes NumericIndex, RangeIndex, DatetimeIndex, TimedeltaIndex, PeriodIndex,
# in which case "kind" is "integer", "integer", "datetime64",
# "timedelta64", and "integer", respectively.
return IndexCol(
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
timedelta_range,
)
import pandas._testing as tm
from pandas.core.api import Int64Index


class TestCategoricalConstructors:
Expand Down Expand Up @@ -74,7 +73,7 @@ def test_constructor_empty(self):
tm.assert_index_equal(c.categories, expected)

c = Categorical([], categories=[1, 2, 3])
expected = Int64Index([1, 2, 3])
expected = Index([1, 2, 3])
tm.assert_index_equal(c.categories, expected)

def test_constructor_empty_boolean(self):
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/arrays/integer/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def test_astype_nansafe():

@pytest.mark.parametrize("dropna", [True, False])
def test_construct_index(all_data, dropna):
# ensure that we do not coerce to Float64Index, rather
# keep as Index
# ensure that we do not coerce to different Index dtype or non-index

all_data = all_data[:10]
if dropna:
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/arrays/sparse/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pandas as pd
from pandas import isna
import pandas._testing as tm
from pandas.core.api import Int64Index
from pandas.core.arrays.sparse import (
SparseArray,
SparseDtype,
Expand Down Expand Up @@ -469,7 +468,7 @@ def test_dropna(fill_value):
tm.assert_sp_array_equal(arr.dropna(), exp)

df = pd.DataFrame({"a": [0, 1], "b": arr})
expected_df = pd.DataFrame({"a": [1], "b": exp}, index=Int64Index([1]))
expected_df = pd.DataFrame({"a": [1], "b": exp}, index=pd.Index([1]))
tm.assert_equal(df.dropna(), expected_df)


Expand Down
9 changes: 1 addition & 8 deletions pandas/tests/base/test_unique.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import pandas as pd
import pandas._testing as tm
from pandas.core.api import NumericIndex
from pandas.tests.base.common import allow_na_ops


Expand All @@ -20,9 +19,6 @@ def test_unique(index_or_series_obj):
expected = pd.MultiIndex.from_tuples(unique_values)
expected.names = obj.names
tm.assert_index_equal(result, expected, exact=True)
elif isinstance(obj, pd.Index) and obj._is_backward_compat_public_numeric_index:
expected = NumericIndex(unique_values, dtype=obj.dtype)
tm.assert_index_equal(result, expected, exact=True)
elif isinstance(obj, pd.Index):
expected = pd.Index(unique_values, dtype=obj.dtype)
if is_datetime64tz_dtype(obj.dtype):
Expand Down Expand Up @@ -58,10 +54,7 @@ def test_unique_null(null_obj, index_or_series_obj):
unique_values_not_null = [val for val in unique_values_raw if not pd.isnull(val)]
unique_values = [null_obj] + unique_values_not_null

if isinstance(obj, pd.Index) and obj._is_backward_compat_public_numeric_index:
expected = NumericIndex(unique_values, dtype=obj.dtype)
tm.assert_index_equal(result, expected, exact=True)
elif isinstance(obj, pd.Index):
if isinstance(obj, pd.Index):
expected = pd.Index(unique_values, dtype=obj.dtype)
if is_datetime64tz_dtype(obj.dtype):
result = result.normalize()
Expand Down
Loading