Skip to content

DEPR: remove Int|Uint|Float64Index from conftest & _testing #49678

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
34 changes: 18 additions & 16 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
is_float_dtype,
is_integer_dtype,
is_sequence,
is_signed_integer_dtype,
is_unsigned_integer_dtype,
pandas_dtype,
)
Expand Down Expand Up @@ -106,12 +107,7 @@
use_numexpr,
with_csv_dialect,
)
from pandas.core.api import (
Float64Index,
Int64Index,
NumericIndex,
UInt64Index,
)
from pandas.core.api import NumericIndex
from pandas.core.arrays import (
BaseMaskedArray,
ExtensionArray,
Expand Down Expand Up @@ -350,7 +346,7 @@ def makeBoolIndex(k: int = 10, name=None) -> Index:
return Index([False, True] + [False] * (k - 2), name=name)


def makeNumericIndex(k: int = 10, name=None, *, dtype) -> NumericIndex:
def makeNumericIndex(k: int = 10, *, name=None, dtype: Dtype | None) -> NumericIndex:
dtype = pandas_dtype(dtype)
assert isinstance(dtype, np.dtype)

Expand All @@ -368,23 +364,29 @@ def makeNumericIndex(k: int = 10, name=None, *, dtype) -> NumericIndex:
return NumericIndex(values, dtype=dtype, name=name)


def makeIntIndex(k: int = 10, name=None) -> Int64Index:
base_idx = makeNumericIndex(k, name=name, dtype="int64")
return Int64Index(base_idx)
def makeIntIndex(k: int = 10, *, name=None, dtype: Dtype = "int64") -> NumericIndex:
dtype = pandas_dtype(dtype)
if not is_signed_integer_dtype(dtype):
raise TypeError(f"Wrong dtype {dtype}")
return makeNumericIndex(k, name=name, dtype=dtype)


def makeUIntIndex(k: int = 10, name=None) -> UInt64Index:
base_idx = makeNumericIndex(k, name=name, dtype="uint64")
return UInt64Index(base_idx)
def makeUIntIndex(k: int = 10, *, name=None, dtype: Dtype = "uint64") -> NumericIndex:
dtype = pandas_dtype(dtype)
if not is_unsigned_integer_dtype(dtype):
raise TypeError(f"Wrong dtype {dtype}")
return makeNumericIndex(k, name=name, dtype=dtype)


def makeRangeIndex(k: int = 10, name=None, **kwargs) -> RangeIndex:
return RangeIndex(0, k, 1, name=name, **kwargs)


def makeFloatIndex(k: int = 10, name=None) -> Float64Index:
base_idx = makeNumericIndex(k, name=name, dtype="float64")
return Float64Index(base_idx)
def makeFloatIndex(k: int = 10, *, name=None, dtype: Dtype = "float64") -> NumericIndex:
dtype = pandas_dtype(dtype)
if not is_float_dtype(dtype):
raise TypeError(f"Wrong dtype {dtype}")
return makeNumericIndex(k, name=name, dtype=dtype)


def makeDateIndex(
Expand Down
33 changes: 16 additions & 17 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,24 +593,21 @@ def _create_mi_with_dt64tz_level():
"datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),
"period": tm.makePeriodIndex(100),
"timedelta": tm.makeTimedeltaIndex(100),
"int": tm.makeIntIndex(100),
"uint": tm.makeUIntIndex(100),
"range": tm.makeRangeIndex(100),
"float": tm.makeFloatIndex(100),
"complex64": tm.makeNumericIndex(100, dtype="float64").astype("complex64"),
"complex128": tm.makeNumericIndex(100, dtype="float64").astype("complex128"),
"num_int64": tm.makeNumericIndex(100, dtype="int64"),
"num_int32": tm.makeNumericIndex(100, dtype="int32"),
"num_int16": tm.makeNumericIndex(100, dtype="int16"),
"num_int8": tm.makeNumericIndex(100, dtype="int8"),
"num_uint64": tm.makeNumericIndex(100, dtype="uint64"),
"num_uint32": tm.makeNumericIndex(100, dtype="uint32"),
"num_uint16": tm.makeNumericIndex(100, dtype="uint16"),
"num_uint8": tm.makeNumericIndex(100, dtype="uint8"),
"num_float64": tm.makeNumericIndex(100, dtype="float64"),
"num_float32": tm.makeNumericIndex(100, dtype="float32"),
"int8": tm.makeIntIndex(100, dtype="int8"),
"int16": tm.makeIntIndex(100, dtype="int16"),
"int32": tm.makeIntIndex(100, dtype="int32"),
"int64": tm.makeIntIndex(100, dtype="int64"),
"uint8": tm.makeUIntIndex(100, dtype="uint8"),
"uint16": tm.makeUIntIndex(100, dtype="uint16"),
"uint32": tm.makeUIntIndex(100, dtype="uint32"),
"uint64": tm.makeUIntIndex(100, dtype="uint64"),
"float32": tm.makeFloatIndex(100, dtype="float32"),
"float64": tm.makeFloatIndex(100, dtype="float64"),
"bool-object": tm.makeBoolIndex(10).astype(object),
"bool-dtype": Index(np.random.randn(10) < 0),
"complex64": tm.makeNumericIndex(100, dtype="float64").astype("complex64"),
"complex128": tm.makeNumericIndex(100, dtype="float64").astype("complex128"),
"categorical": tm.makeCategoricalIndex(100),
"interval": tm.makeIntervalIndex(100),
"empty": Index([]),
Expand Down Expand Up @@ -670,8 +667,10 @@ def index_flat(request):
key
for key, value in indices_dict.items()
if not (
key in ["int", "uint", "range", "empty", "repeats", "bool-dtype"]
or key.startswith("num_")
key.startswith("int")
or key.startswith("uint")
or key.startswith("float")
or key in ["range", "empty", "repeats", "bool-dtype"]
)
and not isinstance(value, MultiIndex)
]
Expand Down
66 changes: 52 additions & 14 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,17 @@ def test_constructor_empty_special(self, empty, klass):
"index",
[
"datetime",
"float",
"int",
"float64",
"float32",
"int64",
"int32",
Comment on lines +345 to +346
Copy link
Member

Choose a reason for hiding this comment

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

just for my understanding, why don't 'int16' and 'int8' need to be here as well? (likewise for other tests)

this isn't a request to change it, just trying to understand

Copy link
Contributor Author

@topper-123 topper-123 Nov 13, 2022

Choose a reason for hiding this comment

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

I thought it was a bit strange to test these things here, and suspect many of the tests here should be located in pandas.tests.indexes.common.Base and adapted in its subclasses as needed. At the same time I didn't really want to delve too deep into refactoring the code base, as it would distract from my main objective right now i.e. getting rid of the old numeric indexes. I probably shouldn't even have added the 32bit versions, but I was curious if it would still pass or some issue would show up.

How about I write an issue about this refactoring and fix it after I've removed Int64Index & friends?

Copy link
Member

Choose a reason for hiding this comment

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

yeah no objections

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've written a issue, see #49679.

"period",
"range",
"repeats",
"timedelta",
"tuples",
"uint",
"uint64",
"uint32",
],
indirect=True,
)
Expand Down Expand Up @@ -375,7 +378,11 @@ def test_view_with_args_object_array_raises(self, index):
with pytest.raises(TypeError, match=msg):
index.view("i8")

@pytest.mark.parametrize("index", ["int", "range"], indirect=True)
@pytest.mark.parametrize(
"index",
["int64", "int32", "range"],
indirect=True,
)
def test_astype(self, index):
casted = index.astype("i8")

Expand Down Expand Up @@ -474,7 +481,11 @@ def test_fancy(self, simple_index):
for i in sl:
assert i == sl[sl.get_loc(i)]

@pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True)
@pytest.mark.parametrize(
"index",
["string", "int64", "int32", "uint64", "uint32", "float64", "float32"],
indirect=True,
)
@pytest.mark.parametrize("dtype", [np.int_, np.bool_])
def test_empty_fancy(self, index, dtype):
empty_arr = np.array([], dtype=dtype)
Expand All @@ -483,7 +494,11 @@ def test_empty_fancy(self, index, dtype):
assert index[[]].identical(empty_index)
assert index[empty_arr].identical(empty_index)

@pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True)
@pytest.mark.parametrize(
"index",
["string", "int64", "int32", "uint64", "uint32", "float64", "float32"],
indirect=True,
)
def test_empty_fancy_raises(self, index):
# DatetimeIndex is excluded, because it overrides getitem and should
# be tested separately.
Expand Down Expand Up @@ -638,9 +653,13 @@ def test_append_empty_preserve_name(self, name, expected):
("bool-object", False),
("bool-dtype", False),
("categorical", False),
("int", True),
("int64", True),
("int32", True),
("uint64", True),
("uint32", True),
("datetime", False),
("float", True),
("float64", True),
("float32", True),
],
indirect=["index"],
)
Expand All @@ -654,9 +673,13 @@ def test_is_numeric(self, index, expected):
("bool-object", True),
("bool-dtype", False),
("categorical", False),
("int", False),
("int64", False),
("int32", False),
("uint64", False),
("uint32", False),
("datetime", False),
("float", False),
("float64", False),
("float32", False),
],
indirect=["index"],
)
Expand Down Expand Up @@ -700,7 +723,9 @@ def test_logical_compat(self, op, simple_index):
index = simple_index
assert getattr(index, op)() == getattr(index.values, op)()

@pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True)
@pytest.mark.parametrize(
"index", ["string", "int64", "int32", "float64", "float32"], indirect=True
)
def test_drop_by_str_label(self, index):
n = len(index)
drop = index[list(range(5, 10))]
Expand All @@ -713,13 +738,17 @@ def test_drop_by_str_label(self, index):
expected = index[1:]
tm.assert_index_equal(dropped, expected)

@pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True)
@pytest.mark.parametrize(
"index", ["string", "int64", "int32", "float64", "float32"], indirect=True
)
@pytest.mark.parametrize("keys", [["foo", "bar"], ["1", "bar"]])
def test_drop_by_str_label_raises_missing_keys(self, index, keys):
with pytest.raises(KeyError, match=""):
index.drop(keys)

@pytest.mark.parametrize("index", ["string", "int", "float"], indirect=True)
@pytest.mark.parametrize(
"index", ["string", "int64", "int32", "float64", "float32"], indirect=True
)
def test_drop_by_str_label_errors_ignore(self, index):
n = len(index)
drop = index[list(range(5, 10))]
Expand Down Expand Up @@ -940,7 +969,16 @@ def test_slice_keep_name(self):

@pytest.mark.parametrize(
"index",
["string", "datetime", "int", "uint", "float"],
[
"string",
"datetime",
"int64",
"int32",
"uint64",
"uint32",
"float64",
"float32",
],
indirect=True,
)
def test_join_self(self, index, join_type):
Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/resample/test_time_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,18 @@ def f(df):


@pytest.mark.parametrize(
"name, func",
"func",
[
("Int64Index", tm.makeIntIndex),
("Index", tm.makeStringIndex),
("Float64Index", tm.makeFloatIndex),
("MultiIndex", lambda m: tm.makeCustomIndex(m, 2)),
tm.makeIntIndex,
tm.makeStringIndex,
tm.makeFloatIndex,
(lambda m: tm.makeCustomIndex(m, 2)),
],
)
def test_fails_on_no_datetime_index(name, func):
def test_fails_on_no_datetime_index(func):
n = 2
index = func(n)
name = type(index).__name__
df = DataFrame({"a": np.random.randn(n)}, index=index)

msg = (
Expand Down
8 changes: 3 additions & 5 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from pandas.core.dtypes.common import (
is_datetime64_dtype,
is_numeric_dtype,
is_period_dtype,
is_timedelta64_dtype,
)
Expand Down Expand Up @@ -130,10 +131,7 @@ def infer_freq(index) -> str | None:
"""
from pandas.core.api import (
DatetimeIndex,
Float64Index,
Index,
Int64Index,
RangeIndex,
)

if isinstance(index, ABCSeries):
Expand Down Expand Up @@ -164,9 +162,9 @@ def infer_freq(index) -> str | None:
return inferer.get_freq()

if isinstance(index, Index) and not isinstance(index, DatetimeIndex):
if isinstance(index, (Int64Index, Float64Index, RangeIndex)):
if is_numeric_dtype(index):
raise TypeError(
f"cannot infer freq from a non-convertible index type {type(index)}"
f"cannot infer freq from a non-convertible index of dtype {index.dtype}"
)
index = index._values

Expand Down