|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas.tests.indexes.conftest import indices_dict |
| 6 | + |
| 7 | + |
| 8 | +def _create_series(index): |
| 9 | + """ Helper for the _series dict """ |
| 10 | + data = np.random.randn(len(index)) |
| 11 | + return pd.Series(data, index=index, name=index.name) |
| 12 | + |
| 13 | + |
| 14 | +_series = { |
| 15 | + f"series-with-{i_id}-index": _create_series(i) for i_id, i in indices_dict.items() |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +def _create_narrow_series(data_dtype): |
| 20 | + """ Helper for the _narrow_series dict """ |
| 21 | + index = indices_dict["int"].copy() |
| 22 | + size = len(index) |
| 23 | + if np.issubdtype(data_dtype, np.float): |
| 24 | + data = np.random.choice(size, size=size, replace=False) |
| 25 | + elif np.issubdtype(data_dtype, np.integer): |
| 26 | + data = np.random.randn(size) |
| 27 | + else: |
| 28 | + raise ValueError(f"Received an unexpected data_dtype: {data_dtype}") |
| 29 | + |
| 30 | + return pd.Series(data.astype(data_dtype), index=index, name="a") |
| 31 | + |
| 32 | + |
| 33 | +_narrow_series = { |
| 34 | + "float32-series": _create_narrow_series(np.float32), |
| 35 | + "int8-series": _create_narrow_series(np.int8), |
| 36 | + "int16-series": _create_narrow_series(np.int16), |
| 37 | + "int32-series": _create_narrow_series(np.int32), |
| 38 | + "uint8-series": _create_narrow_series(np.uint8), |
| 39 | + "uint16-series": _create_narrow_series(np.uint16), |
| 40 | + "uint32-series": _create_narrow_series(np.uint32), |
| 41 | +} |
| 42 | + |
| 43 | +_all_objs = {**indices_dict, **_series, **_narrow_series} |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(params=_all_objs.keys()) |
| 47 | +def index_or_series_obj(request): |
| 48 | + """ |
| 49 | + Fixture for tests on indexes, series and series with a narrow dtype |
| 50 | + copy to avoid mutation, e.g. setting .name |
| 51 | + """ |
| 52 | + return _all_objs[request.param].copy(deep=True) |
0 commit comments