Skip to content

Commit 44b6cb9

Browse files
authored
TST/CLN: Remove makeFloat/Period/Int/NumericIndex (#56229)
* Remove makeFloatIndex * Remove makePeriodIndex * Remove makeIntIndex/makeNumericIndex * Avoid indirect imports
1 parent 1aa9aea commit 44b6cb9

File tree

13 files changed

+65
-90
lines changed

13 files changed

+65
-90
lines changed

asv_bench/benchmarks/arithmetic.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
import pandas as pd
77
from pandas import (
88
DataFrame,
9+
Index,
910
Series,
1011
Timestamp,
1112
date_range,
1213
to_timedelta,
1314
)
14-
import pandas._testing as tm
1515
from pandas.core.algorithms import checked_add_with_arr
1616

1717
from .pandas_vb_common import numeric_dtypes
@@ -323,8 +323,10 @@ class IndexArithmetic:
323323

324324
def setup(self, dtype):
325325
N = 10**6
326-
indexes = {"int": "makeIntIndex", "float": "makeFloatIndex"}
327-
self.index = getattr(tm, indexes[dtype])(N)
326+
if dtype == "float":
327+
self.index = Index(np.arange(N), dtype=np.float64)
328+
elif dtype == "int":
329+
self.index = Index(np.arange(N), dtype=np.int64)
328330

329331
def time_add(self, dtype):
330332
self.index + 2

pandas/_testing/__init__.py

+6-51
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@
2727
from pandas.compat import pa_version_under10p1
2828

2929
from pandas.core.dtypes.common import (
30-
is_float_dtype,
3130
is_sequence,
32-
is_signed_integer_dtype,
3331
is_string_dtype,
34-
is_unsigned_integer_dtype,
35-
pandas_dtype,
3632
)
3733

3834
import pandas as pd
@@ -46,6 +42,8 @@
4642
RangeIndex,
4743
Series,
4844
bdate_range,
45+
date_range,
46+
period_range,
4947
timedelta_range,
5048
)
5149
from pandas._testing._io import (
@@ -111,7 +109,6 @@
111109
NpDtype,
112110
)
113111

114-
from pandas import PeriodIndex
115112
from pandas.core.arrays import ArrowExtensionArray
116113

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

353350

354-
def makeNumericIndex(k: int = 10, *, name=None, dtype: Dtype | None) -> Index:
355-
dtype = pandas_dtype(dtype)
356-
assert isinstance(dtype, np.dtype)
357-
358-
if dtype.kind in "iu":
359-
values = np.arange(k, dtype=dtype)
360-
if is_unsigned_integer_dtype(dtype):
361-
values += 2 ** (dtype.itemsize * 8 - 1)
362-
elif dtype.kind == "f":
363-
values = np.random.default_rng(2).random(k) - np.random.default_rng(2).random(1)
364-
values.sort()
365-
values = values * (10 ** np.random.default_rng(2).integers(0, 9))
366-
else:
367-
raise NotImplementedError(f"wrong dtype {dtype}")
368-
369-
return Index(values, dtype=dtype, name=name)
370-
371-
372-
def makeIntIndex(k: int = 10, *, name=None, dtype: Dtype = "int64") -> Index:
373-
dtype = pandas_dtype(dtype)
374-
if not is_signed_integer_dtype(dtype):
375-
raise TypeError(f"Wrong dtype {dtype}")
376-
return makeNumericIndex(k, name=name, dtype=dtype)
377-
378-
379-
def makeFloatIndex(k: int = 10, *, name=None, dtype: Dtype = "float64") -> Index:
380-
dtype = pandas_dtype(dtype)
381-
if not is_float_dtype(dtype):
382-
raise TypeError(f"Wrong dtype {dtype}")
383-
return makeNumericIndex(k, name=name, dtype=dtype)
384-
385-
386351
def makeDateIndex(
387352
k: int = 10, freq: Frequency = "B", name=None, **kwargs
388353
) -> DatetimeIndex:
@@ -391,12 +356,6 @@ def makeDateIndex(
391356
return DatetimeIndex(dr, name=name, **kwargs)
392357

393358

394-
def makePeriodIndex(k: int = 10, name=None, **kwargs) -> PeriodIndex:
395-
dt = datetime(2000, 1, 1)
396-
pi = pd.period_range(start=dt, periods=k, freq="D", name=name, **kwargs)
397-
return pi
398-
399-
400359
def makeObjectSeries(name=None) -> Series:
401360
data = [f"foo_{i}" for i in range(_N)]
402361
index = Index([f"bar_{i}" for i in range(_N)])
@@ -487,12 +446,12 @@ def makeCustomIndex(
487446

488447
# specific 1D index type requested?
489448
idx_func_dict: dict[str, Callable[..., Index]] = {
490-
"i": makeIntIndex,
491-
"f": makeFloatIndex,
449+
"i": lambda n: Index(np.arange(n), dtype=np.int64),
450+
"f": lambda n: Index(np.arange(n), dtype=np.float64),
492451
"s": lambda n: Index([f"{i}_{chr(i)}" for i in range(97, 97 + n)]),
493-
"dt": makeDateIndex,
452+
"dt": lambda n: date_range("2020-01-01", periods=n),
494453
"td": lambda n: timedelta_range("1 day", periods=n),
495-
"p": makePeriodIndex,
454+
"p": lambda n: period_range("2020-01-01", periods=n, freq="D"),
496455
}
497456
idx_func = idx_func_dict.get(idx_type)
498457
if idx_func:
@@ -975,11 +934,7 @@ def shares_memory(left, right) -> bool:
975934
"makeCustomIndex",
976935
"makeDataFrame",
977936
"makeDateIndex",
978-
"makeFloatIndex",
979-
"makeIntIndex",
980-
"makeNumericIndex",
981937
"makeObjectSeries",
982-
"makePeriodIndex",
983938
"makeTimeDataFrame",
984939
"makeTimeSeries",
985940
"maybe_produces_warning",

pandas/conftest.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
Series,
6969
Timedelta,
7070
Timestamp,
71+
period_range,
7172
timedelta_range,
7273
)
7374
import pandas._testing as tm
@@ -616,23 +617,27 @@ def _create_mi_with_dt64tz_level():
616617
"string": Index([f"pandas_{i}" for i in range(100)]),
617618
"datetime": tm.makeDateIndex(100),
618619
"datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),
619-
"period": tm.makePeriodIndex(100),
620+
"period": period_range("2020-01-01", periods=100, freq="D"),
620621
"timedelta": timedelta_range(start="1 day", periods=100, freq="D"),
621622
"range": RangeIndex(100),
622-
"int8": tm.makeIntIndex(100, dtype="int8"),
623-
"int16": tm.makeIntIndex(100, dtype="int16"),
624-
"int32": tm.makeIntIndex(100, dtype="int32"),
625-
"int64": tm.makeIntIndex(100, dtype="int64"),
623+
"int8": Index(np.arange(100), dtype="int8"),
624+
"int16": Index(np.arange(100), dtype="int16"),
625+
"int32": Index(np.arange(100), dtype="int32"),
626+
"int64": Index(np.arange(100), dtype="int64"),
626627
"uint8": Index(np.arange(100), dtype="uint8"),
627628
"uint16": Index(np.arange(100), dtype="uint16"),
628629
"uint32": Index(np.arange(100), dtype="uint32"),
629630
"uint64": Index(np.arange(100), dtype="uint64"),
630-
"float32": tm.makeFloatIndex(100, dtype="float32"),
631-
"float64": tm.makeFloatIndex(100, dtype="float64"),
631+
"float32": Index(np.arange(100), dtype="float32"),
632+
"float64": Index(np.arange(100), dtype="float64"),
632633
"bool-object": Index([True, False] * 5, dtype=object),
633634
"bool-dtype": Index(np.random.default_rng(2).standard_normal(10) < 0),
634-
"complex64": tm.makeNumericIndex(100, dtype="float64").astype("complex64"),
635-
"complex128": tm.makeNumericIndex(100, dtype="float64").astype("complex128"),
635+
"complex64": Index(
636+
np.arange(100, dtype="complex64") + 1.0j * np.arange(100, dtype="complex64")
637+
),
638+
"complex128": Index(
639+
np.arange(100, dtype="complex128") + 1.0j * np.arange(100, dtype="complex128")
640+
),
636641
"categorical": CategoricalIndex(list("abcd") * 25),
637642
"interval": IntervalIndex.from_breaks(np.linspace(0, 100, num=101)),
638643
"empty": Index([]),

pandas/tests/extension/test_masked.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
)
2525
from pandas.compat.numpy import np_version_gt2
2626

27+
from pandas.core.dtypes.common import (
28+
is_float_dtype,
29+
is_signed_integer_dtype,
30+
is_unsigned_integer_dtype,
31+
)
32+
2733
import pandas as pd
2834
import pandas._testing as tm
2935
from pandas.core.arrays.boolean import BooleanDtype
@@ -281,23 +287,23 @@ def check_reduce(self, ser: pd.Series, op_name: str, skipna: bool):
281287
tm.assert_almost_equal(result, expected)
282288

283289
def _get_expected_reduction_dtype(self, arr, op_name: str, skipna: bool):
284-
if tm.is_float_dtype(arr.dtype):
290+
if is_float_dtype(arr.dtype):
285291
cmp_dtype = arr.dtype.name
286292
elif op_name in ["mean", "median", "var", "std", "skew"]:
287293
cmp_dtype = "Float64"
288294
elif op_name in ["max", "min"]:
289295
cmp_dtype = arr.dtype.name
290296
elif arr.dtype in ["Int64", "UInt64"]:
291297
cmp_dtype = arr.dtype.name
292-
elif tm.is_signed_integer_dtype(arr.dtype):
298+
elif is_signed_integer_dtype(arr.dtype):
293299
# TODO: Why does Window Numpy 2.0 dtype depend on skipna?
294300
cmp_dtype = (
295301
"Int32"
296302
if (is_platform_windows() and (not np_version_gt2 or not skipna))
297303
or not IS64
298304
else "Int64"
299305
)
300-
elif tm.is_unsigned_integer_dtype(arr.dtype):
306+
elif is_unsigned_integer_dtype(arr.dtype):
301307
cmp_dtype = (
302308
"UInt32"
303309
if (is_platform_windows() and (not np_version_gt2 or not skipna))

pandas/tests/indexes/interval/test_constructors.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.core.dtypes.common import is_unsigned_integer_dtype
67
from pandas.core.dtypes.dtypes import IntervalDtype
78

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

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

394395
if len(breaks) == 0:

pandas/tests/indexes/test_base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ def test_map_with_tuples(self):
507507

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

@@ -555,7 +555,7 @@ def test_map_tseries_indices_accsr_return_index(self):
555555
def test_map_dictlike_simple(self, mapper):
556556
# GH 12756
557557
expected = Index(["foo", "bar", "baz"])
558-
index = tm.makeIntIndex(3)
558+
index = Index(np.arange(3), dtype=np.int64)
559559
result = index.map(mapper(expected.values, index))
560560
tm.assert_index_equal(result, expected)
561561

pandas/tests/io/pytables/test_time_series.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
DatetimeIndex,
99
Series,
1010
_testing as tm,
11+
period_range,
1112
)
1213
from pandas.tests.io.pytables.common import ensure_clean_store
1314

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

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

63-
idx = tm.makePeriodIndex(10)
64+
idx = period_range("2020-01-01", periods=10, freq="D")
6465
df = DataFrame(np.random.default_rng(2).standard_normal((len(idx), 3)), idx)
6566
store["a"] = df
6667
result = store["a"]

pandas/tests/reductions/test_reductions.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Timestamp,
2424
date_range,
2525
isna,
26+
period_range,
2627
timedelta_range,
2728
to_timedelta,
2829
)
@@ -34,11 +35,13 @@
3435
def get_objs():
3536
indexes = [
3637
Index([True, False] * 5, name="a"),
37-
tm.makeIntIndex(10, name="a"),
38-
tm.makeFloatIndex(10, name="a"),
39-
tm.makeDateIndex(10, name="a"),
40-
tm.makeDateIndex(10, name="a").tz_localize(tz="US/Eastern"),
41-
tm.makePeriodIndex(10, name="a"),
38+
Index(np.arange(10), dtype=np.int64, name="a"),
39+
Index(np.arange(10), dtype=np.float64, name="a"),
40+
DatetimeIndex(date_range("2020-01-01", periods=10), name="a"),
41+
DatetimeIndex(date_range("2020-01-01", periods=10), name="a").tz_localize(
42+
tz="US/Eastern"
43+
),
44+
PeriodIndex(period_range("2020-01-01", periods=10, freq="D"), name="a"),
4245
Index([str(i) for i in range(10)], name="a"),
4346
]
4447

@@ -534,7 +537,7 @@ def test_minmax_period_empty_nat(self, op, data):
534537
assert result is NaT
535538

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

539542
assert np.min(pr) == Period("2016-01-15", freq="D")
540543
assert np.max(pr) == Period("2016-01-20", freq="D")

pandas/tests/series/indexing/test_setitem.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,9 @@ def test_setitem_slice_integers(self):
240240

241241
def test_setitem_slicestep(self):
242242
# caught this bug when writing tests
243-
series = Series(tm.makeIntIndex(20).astype(float), index=tm.makeIntIndex(20))
243+
series = Series(
244+
np.arange(20, dtype=np.float64), index=np.arange(20, dtype=np.int64)
245+
)
244246

245247
series[::2] = 0
246248
assert (series[::2] == 0).all()

pandas/tests/series/methods/test_combine_first.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_combine_first_name(self, datetime_series):
3232
assert result.name == datetime_series.name
3333

3434
def test_combine_first(self):
35-
values = tm.makeIntIndex(20).values.astype(float)
36-
series = Series(values, index=tm.makeIntIndex(20))
35+
values = np.arange(20, dtype=np.float64)
36+
series = Series(values, index=np.arange(20, dtype=np.int64))
3737

3838
series_copy = series * 2
3939
series_copy[::2] = np.nan

pandas/tests/series/test_api.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Index,
1111
Series,
1212
date_range,
13+
period_range,
1314
timedelta_range,
1415
)
1516
import pandas._testing as tm
@@ -72,13 +73,12 @@ def test_tab_completion_with_categorical(self):
7273
Index(list("ab") * 5, dtype="category"),
7374
Index([str(i) for i in range(10)]),
7475
Index(["foo", "bar", "baz"] * 2),
75-
tm.makeDateIndex(10),
76-
tm.makePeriodIndex(10),
76+
date_range("2020-01-01", periods=10),
77+
period_range("2020-01-01", periods=10, freq="D"),
7778
timedelta_range("1 day", periods=10),
78-
tm.makeIntIndex(10),
7979
Index(np.arange(10), dtype=np.uint64),
80-
tm.makeIntIndex(10),
81-
tm.makeFloatIndex(10),
80+
Index(np.arange(10), dtype=np.int64),
81+
Index(np.arange(10), dtype=np.float64),
8282
Index([True, False]),
8383
Index([f"a{i}" for i in range(101)]),
8484
pd.MultiIndex.from_tuples(zip("ABCD", "EFGH")),

pandas/tests/series/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ def test_constructor_dict(self):
13371337
expected = Series([1, 2, np.nan, 0], index=["b", "c", "d", "a"])
13381338
tm.assert_series_equal(result, expected)
13391339

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

0 commit comments

Comments
 (0)