Skip to content

Commit c3cc757

Browse files
moved indices fixture to pandas/conftest and creating index_or_series_obj fixture there as well
1 parent ac4a5eb commit c3cc757

File tree

5 files changed

+76
-83
lines changed

5 files changed

+76
-83
lines changed

pandas/conftest.py

+74
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from pandas import DataFrame
1818
import pandas._testing as tm
1919
from pandas.core import ops
20+
from pandas.core.indexes.api import Index, MultiIndex
2021

2122
hypothesis.settings.register_profile(
2223
"ci",
@@ -953,3 +954,76 @@ def __len__(self):
953954
return self._data.__len__()
954955

955956
return TestNonDictMapping
957+
958+
959+
indices_dict = {
960+
"unicode": tm.makeUnicodeIndex(100),
961+
"string": tm.makeStringIndex(100),
962+
"datetime": tm.makeDateIndex(100),
963+
"datetime-tz": tm.makeDateIndex(100, tz="US/Pacific"),
964+
"period": tm.makePeriodIndex(100),
965+
"timedelta": tm.makeTimedeltaIndex(100),
966+
"int": tm.makeIntIndex(100),
967+
"uint": tm.makeUIntIndex(100),
968+
"range": tm.makeRangeIndex(100),
969+
"float": tm.makeFloatIndex(100),
970+
"bool": tm.makeBoolIndex(2),
971+
"categorical": tm.makeCategoricalIndex(100),
972+
"interval": tm.makeIntervalIndex(100),
973+
"empty": Index([]),
974+
"tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])),
975+
"repeats": Index([0, 0, 1, 1, 2, 2]),
976+
}
977+
978+
979+
@pytest.fixture(params=indices_dict.keys())
980+
def indices(request):
981+
# copy to avoid mutation, e.g. setting .name
982+
return indices_dict[request.param].copy()
983+
984+
985+
def _create_series(index):
986+
""" Helper for the _series dict """
987+
data = np.random.randn(len(index))
988+
return pd.Series(data, index=index, name=index.name)
989+
990+
991+
_series = {
992+
f"series-with-{id_}-index": _create_series(index)
993+
for id_, index in indices_dict.items()
994+
}
995+
996+
997+
def _create_narrow_series(dtype):
998+
""" Helper for the _narrow_series dict """
999+
index = indices_dict["int"].copy()
1000+
size = len(index)
1001+
if np.issubdtype(dtype, np.floating):
1002+
data = np.random.choice(size, size=size, replace=False)
1003+
elif np.issubdtype(dtype, np.integer):
1004+
data = np.random.randn(size)
1005+
else:
1006+
raise ValueError(f"Received an unexpected data_dtype: {dtype}")
1007+
return pd.Series(data.astype(dtype), index=index, name="a")
1008+
1009+
1010+
_narrow_series = {
1011+
"float32-series": _create_narrow_series(np.float32),
1012+
"int8-series": _create_narrow_series(np.int8),
1013+
"int16-series": _create_narrow_series(np.int16),
1014+
"int32-series": _create_narrow_series(np.int32),
1015+
"uint8-series": _create_narrow_series(np.uint8),
1016+
"uint16-series": _create_narrow_series(np.uint16),
1017+
"uint32-series": _create_narrow_series(np.uint32),
1018+
}
1019+
1020+
_index_or_series_objs = {**indices_dict, **_series, **_narrow_series}
1021+
1022+
1023+
@pytest.fixture(params=_index_or_series_objs.keys())
1024+
def index_or_series_obj(request):
1025+
"""
1026+
Fixture for tests on indexes, series and series with a narrow dtype
1027+
copy to avoid mutation, e.g. setting .name
1028+
"""
1029+
return _index_or_series_objs[request.param].copy(deep=True)

pandas/tests/base/conftest.py

-52
This file was deleted.

pandas/tests/indexes/conftest.py

-29
This file was deleted.

pandas/tests/indexes/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
period_range,
3535
)
3636
import pandas._testing as tm
37+
from pandas.conftest import indices_dict
3738
from pandas.core.indexes.api import (
3839
Index,
3940
MultiIndex,
@@ -42,7 +43,6 @@
4243
ensure_index_from_sequences,
4344
)
4445
from pandas.tests.indexes.common import Base
45-
from pandas.tests.indexes.conftest import indices_dict
4646

4747

4848
class TestIndex(Base):

pandas/tests/indexes/test_setops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from pandas import Float64Index, Int64Index, RangeIndex, UInt64Index
1414
import pandas._testing as tm
1515
from pandas.api.types import pandas_dtype
16-
from pandas.tests.indexes.conftest import indices_dict
16+
from pandas.conftest import indices_dict
1717

1818
COMPATIBLE_INCONSISTENT_PAIRS = {
1919
(Int64Index, RangeIndex): (tm.makeIntIndex, tm.makeRangeIndex),

0 commit comments

Comments
 (0)