Skip to content

TST/CLN: Remove makeFloat/Period/Int/NumericIndex #56229

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
merged 5 commits into from
Nov 29, 2023
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
8 changes: 5 additions & 3 deletions asv_bench/benchmarks/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import pandas as pd
from pandas import (
DataFrame,
Index,
Series,
Timestamp,
date_range,
to_timedelta,
)
import pandas._testing as tm
from pandas.core.algorithms import checked_add_with_arr

from .pandas_vb_common import numeric_dtypes
Expand Down Expand Up @@ -323,8 +323,10 @@ class IndexArithmetic:

def setup(self, dtype):
N = 10**6
indexes = {"int": "makeIntIndex", "float": "makeFloatIndex"}
self.index = getattr(tm, indexes[dtype])(N)
if dtype == "float":
self.index = Index(np.arange(N), dtype=np.float64)
elif dtype == "int":
self.index = Index(np.arange(N), dtype=np.int64)

def time_add(self, dtype):
self.index + 2
Expand Down
57 changes: 6 additions & 51 deletions pandas/_testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@
from pandas.compat import pa_version_under10p1

from pandas.core.dtypes.common import (
is_float_dtype,
is_sequence,
is_signed_integer_dtype,
is_string_dtype,
is_unsigned_integer_dtype,
pandas_dtype,
)

import pandas as pd
Expand All @@ -46,6 +42,8 @@
RangeIndex,
Series,
bdate_range,
date_range,
period_range,
timedelta_range,
)
from pandas._testing._io import (
Expand Down Expand Up @@ -111,7 +109,6 @@
NpDtype,
)

from pandas import PeriodIndex
from pandas.core.arrays import ArrowExtensionArray

_N = 30
Expand Down Expand Up @@ -351,38 +348,6 @@ def getCols(k) -> str:
return string.ascii_uppercase[:k]


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

if dtype.kind in "iu":
values = np.arange(k, dtype=dtype)
if is_unsigned_integer_dtype(dtype):
values += 2 ** (dtype.itemsize * 8 - 1)
elif dtype.kind == "f":
values = np.random.default_rng(2).random(k) - np.random.default_rng(2).random(1)
values.sort()
values = values * (10 ** np.random.default_rng(2).integers(0, 9))
else:
raise NotImplementedError(f"wrong dtype {dtype}")

return Index(values, dtype=dtype, name=name)


def makeIntIndex(k: int = 10, *, name=None, dtype: Dtype = "int64") -> Index:
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 makeFloatIndex(k: int = 10, *, name=None, dtype: Dtype = "float64") -> Index:
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(
k: int = 10, freq: Frequency = "B", name=None, **kwargs
) -> DatetimeIndex:
Expand All @@ -391,12 +356,6 @@ def makeDateIndex(
return DatetimeIndex(dr, name=name, **kwargs)


def makePeriodIndex(k: int = 10, name=None, **kwargs) -> PeriodIndex:
dt = datetime(2000, 1, 1)
pi = pd.period_range(start=dt, periods=k, freq="D", name=name, **kwargs)
return pi


def makeObjectSeries(name=None) -> Series:
data = [f"foo_{i}" for i in range(_N)]
index = Index([f"bar_{i}" for i in range(_N)])
Expand Down Expand Up @@ -487,12 +446,12 @@ def makeCustomIndex(

# specific 1D index type requested?
idx_func_dict: dict[str, Callable[..., Index]] = {
"i": makeIntIndex,
"f": makeFloatIndex,
"i": lambda n: Index(np.arange(n), dtype=np.int64),
"f": lambda n: Index(np.arange(n), dtype=np.float64),
"s": lambda n: Index([f"{i}_{chr(i)}" for i in range(97, 97 + n)]),
"dt": makeDateIndex,
"dt": lambda n: date_range("2020-01-01", periods=n),
"td": lambda n: timedelta_range("1 day", periods=n),
"p": makePeriodIndex,
"p": lambda n: period_range("2020-01-01", periods=n, freq="D"),
}
idx_func = idx_func_dict.get(idx_type)
if idx_func:
Expand Down Expand Up @@ -975,11 +934,7 @@ def shares_memory(left, right) -> bool:
"makeCustomIndex",
"makeDataFrame",
"makeDateIndex",
"makeFloatIndex",
"makeIntIndex",
"makeNumericIndex",
"makeObjectSeries",
"makePeriodIndex",
"makeTimeDataFrame",
"makeTimeSeries",
"maybe_produces_warning",
Expand Down
23 changes: 14 additions & 9 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
Series,
Timedelta,
Timestamp,
period_range,
timedelta_range,
)
import pandas._testing as tm
Expand Down Expand Up @@ -616,23 +617,27 @@ def _create_mi_with_dt64tz_level():
"string": Index([f"pandas_{i}" for i in range(100)]),
"datetime": tm.makeDateIndex(100),
"datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),
"period": tm.makePeriodIndex(100),
"period": period_range("2020-01-01", periods=100, freq="D"),
"timedelta": timedelta_range(start="1 day", periods=100, freq="D"),
"range": RangeIndex(100),
"int8": tm.makeIntIndex(100, dtype="int8"),
"int16": tm.makeIntIndex(100, dtype="int16"),
"int32": tm.makeIntIndex(100, dtype="int32"),
"int64": tm.makeIntIndex(100, dtype="int64"),
"int8": Index(np.arange(100), dtype="int8"),
"int16": Index(np.arange(100), dtype="int16"),
"int32": Index(np.arange(100), dtype="int32"),
"int64": Index(np.arange(100), dtype="int64"),
"uint8": Index(np.arange(100), dtype="uint8"),
"uint16": Index(np.arange(100), dtype="uint16"),
"uint32": Index(np.arange(100), dtype="uint32"),
"uint64": Index(np.arange(100), dtype="uint64"),
"float32": tm.makeFloatIndex(100, dtype="float32"),
"float64": tm.makeFloatIndex(100, dtype="float64"),
"float32": Index(np.arange(100), dtype="float32"),
"float64": Index(np.arange(100), dtype="float64"),
"bool-object": Index([True, False] * 5, dtype=object),
"bool-dtype": Index(np.random.default_rng(2).standard_normal(10) < 0),
"complex64": tm.makeNumericIndex(100, dtype="float64").astype("complex64"),
"complex128": tm.makeNumericIndex(100, dtype="float64").astype("complex128"),
"complex64": Index(
np.arange(100, dtype="complex64") + 1.0j * np.arange(100, dtype="complex64")
),
"complex128": Index(
np.arange(100, dtype="complex128") + 1.0j * np.arange(100, dtype="complex128")
),
"categorical": CategoricalIndex(list("abcd") * 25),
"interval": IntervalIndex.from_breaks(np.linspace(0, 100, num=101)),
"empty": Index([]),
Expand Down
12 changes: 9 additions & 3 deletions pandas/tests/extension/test_masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
)
from pandas.compat.numpy import np_version_gt2

from pandas.core.dtypes.common import (
is_float_dtype,
is_signed_integer_dtype,
is_unsigned_integer_dtype,
)

import pandas as pd
import pandas._testing as tm
from pandas.core.arrays.boolean import BooleanDtype
Expand Down Expand Up @@ -281,23 +287,23 @@ def check_reduce(self, ser: pd.Series, op_name: str, skipna: bool):
tm.assert_almost_equal(result, expected)

def _get_expected_reduction_dtype(self, arr, op_name: str, skipna: bool):
if tm.is_float_dtype(arr.dtype):
if is_float_dtype(arr.dtype):
cmp_dtype = arr.dtype.name
elif op_name in ["mean", "median", "var", "std", "skew"]:
cmp_dtype = "Float64"
elif op_name in ["max", "min"]:
cmp_dtype = arr.dtype.name
elif arr.dtype in ["Int64", "UInt64"]:
cmp_dtype = arr.dtype.name
elif tm.is_signed_integer_dtype(arr.dtype):
elif is_signed_integer_dtype(arr.dtype):
# TODO: Why does Window Numpy 2.0 dtype depend on skipna?
cmp_dtype = (
"Int32"
if (is_platform_windows() and (not np_version_gt2 or not skipna))
or not IS64
else "Int64"
)
elif tm.is_unsigned_integer_dtype(arr.dtype):
elif is_unsigned_integer_dtype(arr.dtype):
cmp_dtype = (
"UInt32"
if (is_platform_windows() and (not np_version_gt2 or not skipna))
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/interval/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest

from pandas.core.dtypes.common import is_unsigned_integer_dtype
from pandas.core.dtypes.dtypes import IntervalDtype

from pandas import (
Expand Down Expand Up @@ -330,7 +331,7 @@ def get_kwargs_from_breaks(self, breaks, closed="right"):
converts intervals in breaks format to a dictionary of kwargs to
specific to the format expected by IntervalIndex.from_tuples
"""
if tm.is_unsigned_integer_dtype(breaks):
if is_unsigned_integer_dtype(breaks):
pytest.skip(f"{breaks.dtype} not relevant IntervalIndex.from_tuples tests")

if len(breaks) == 0:
Expand Down Expand Up @@ -388,7 +389,7 @@ def get_kwargs_from_breaks(self, breaks, closed="right"):
converts intervals in breaks format to a dictionary of kwargs to
specific to the format expected by the IntervalIndex/Index constructors
"""
if tm.is_unsigned_integer_dtype(breaks):
if is_unsigned_integer_dtype(breaks):
pytest.skip(f"{breaks.dtype} not relevant for class constructor tests")

if len(breaks) == 0:
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ def test_map_with_tuples(self):

# Test that returning a single tuple from an Index
# returns an Index.
index = tm.makeIntIndex(3)
result = tm.makeIntIndex(3).map(lambda x: (x,))
index = Index(np.arange(3), dtype=np.int64)
result = index.map(lambda x: (x,))
expected = Index([(i,) for i in index])
tm.assert_index_equal(result, expected)

Expand Down Expand Up @@ -555,7 +555,7 @@ def test_map_tseries_indices_accsr_return_index(self):
def test_map_dictlike_simple(self, mapper):
# GH 12756
expected = Index(["foo", "bar", "baz"])
index = tm.makeIntIndex(3)
index = Index(np.arange(3), dtype=np.int64)
result = index.map(mapper(expected.values, index))
tm.assert_index_equal(result, expected)

Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/io/pytables/test_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DatetimeIndex,
Series,
_testing as tm,
period_range,
)
from pandas.tests.io.pytables.common import ensure_clean_store

Expand Down Expand Up @@ -36,7 +37,7 @@ def test_tseries_indices_series(setup_path):
assert result.index.freq == ser.index.freq
tm.assert_class_equal(result.index, ser.index, obj="series index")

idx = tm.makePeriodIndex(10)
idx = period_range("2020-01-01", periods=10, freq="D")
ser = Series(np.random.default_rng(2).standard_normal(len(idx)), idx)
store["a"] = ser
result = store["a"]
Expand All @@ -60,7 +61,7 @@ def test_tseries_indices_frame(setup_path):
assert result.index.freq == df.index.freq
tm.assert_class_equal(result.index, df.index, obj="dataframe index")

idx = tm.makePeriodIndex(10)
idx = period_range("2020-01-01", periods=10, freq="D")
df = DataFrame(np.random.default_rng(2).standard_normal((len(idx), 3)), idx)
store["a"] = df
result = store["a"]
Expand Down
15 changes: 9 additions & 6 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Timestamp,
date_range,
isna,
period_range,
timedelta_range,
to_timedelta,
)
Expand All @@ -34,11 +35,13 @@
def get_objs():
indexes = [
Index([True, False] * 5, name="a"),
tm.makeIntIndex(10, name="a"),
tm.makeFloatIndex(10, name="a"),
tm.makeDateIndex(10, name="a"),
tm.makeDateIndex(10, name="a").tz_localize(tz="US/Eastern"),
tm.makePeriodIndex(10, name="a"),
Index(np.arange(10), dtype=np.int64, name="a"),
Index(np.arange(10), dtype=np.float64, name="a"),
DatetimeIndex(date_range("2020-01-01", periods=10), name="a"),
DatetimeIndex(date_range("2020-01-01", periods=10), name="a").tz_localize(
tz="US/Eastern"
),
PeriodIndex(period_range("2020-01-01", periods=10, freq="D"), name="a"),
Index([str(i) for i in range(10)], name="a"),
]

Expand Down Expand Up @@ -534,7 +537,7 @@ def test_minmax_period_empty_nat(self, op, data):
assert result is NaT

def test_numpy_minmax_period(self):
pr = pd.period_range(start="2016-01-15", end="2016-01-20")
pr = period_range(start="2016-01-15", end="2016-01-20")

assert np.min(pr) == Period("2016-01-15", freq="D")
assert np.max(pr) == Period("2016-01-20", freq="D")
Expand Down
4 changes: 3 additions & 1 deletion pandas/tests/series/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def test_setitem_slice_integers(self):

def test_setitem_slicestep(self):
# caught this bug when writing tests
series = Series(tm.makeIntIndex(20).astype(float), index=tm.makeIntIndex(20))
series = Series(
np.arange(20, dtype=np.float64), index=np.arange(20, dtype=np.int64)
)

series[::2] = 0
assert (series[::2] == 0).all()
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/methods/test_combine_first.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_combine_first_name(self, datetime_series):
assert result.name == datetime_series.name

def test_combine_first(self):
values = tm.makeIntIndex(20).values.astype(float)
series = Series(values, index=tm.makeIntIndex(20))
values = np.arange(20, dtype=np.float64)
series = Series(values, index=np.arange(20, dtype=np.int64))

series_copy = series * 2
series_copy[::2] = np.nan
Expand Down
10 changes: 5 additions & 5 deletions pandas/tests/series/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Index,
Series,
date_range,
period_range,
timedelta_range,
)
import pandas._testing as tm
Expand Down Expand Up @@ -72,13 +73,12 @@ def test_tab_completion_with_categorical(self):
Index(list("ab") * 5, dtype="category"),
Index([str(i) for i in range(10)]),
Index(["foo", "bar", "baz"] * 2),
tm.makeDateIndex(10),
tm.makePeriodIndex(10),
date_range("2020-01-01", periods=10),
period_range("2020-01-01", periods=10, freq="D"),
timedelta_range("1 day", periods=10),
tm.makeIntIndex(10),
Index(np.arange(10), dtype=np.uint64),
tm.makeIntIndex(10),
tm.makeFloatIndex(10),
Index(np.arange(10), dtype=np.int64),
Index(np.arange(10), dtype=np.float64),
Index([True, False]),
Index([f"a{i}" for i in range(101)]),
pd.MultiIndex.from_tuples(zip("ABCD", "EFGH")),
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ def test_constructor_dict(self):
expected = Series([1, 2, np.nan, 0], index=["b", "c", "d", "a"])
tm.assert_series_equal(result, expected)

pidx = tm.makePeriodIndex(100)
pidx = period_range("2020-01-01", periods=10, freq="D")
d = {pidx[0]: 0, pidx[1]: 1}
result = Series(d, index=pidx)
expected = Series(np.nan, pidx, dtype=np.float64)
Expand Down
Loading