From 3a9802db445b59e4b3c873ea17da614cda6ace9a Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Wed, 5 Feb 2020 15:26:58 +0100 Subject: [PATCH 1/4] started to fixturize pandas/tests/base --- pandas/tests/base/conftest.py | 52 +++++++++++++++++++++++++++++++++++ pandas/tests/base/test_ops.py | 30 ++++++++++---------- 2 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 pandas/tests/base/conftest.py diff --git a/pandas/tests/base/conftest.py b/pandas/tests/base/conftest.py new file mode 100644 index 0000000000000..12337b3b982c6 --- /dev/null +++ b/pandas/tests/base/conftest.py @@ -0,0 +1,52 @@ +import numpy as np +import pytest + +import pandas as pd +from pandas.tests.indexes.conftest import indices_dict + + +def _create_series(index): + """ Helper for the _series dict """ + data = np.random.randn(len(index)) + return pd.Series(data, index=index, name=index.name) + + +_series = { + f"series-with-{i_id}-index": _create_series(i) for i_id, i in indices_dict.items() +} + + +def _create_narrow_series(data_dtype): + """ Helper for the _narrow_series dict """ + index = indices_dict["int"].copy() + size = len(index) + if np.issubdtype(data_dtype, np.float): + data = np.random.choice(size, size=size, replace=False) + elif np.issubdtype(data_dtype, np.integer): + data = np.random.randn(size) + else: + raise ValueError(f"Received an unexpected data_dtype: {data_dtype}") + + return pd.Series(data.astype(data_dtype), index=index, name="a") + + +_narrow_series = { + "float32-series": _create_narrow_series(np.float32), + "int8-series": _create_narrow_series(np.int8), + "int16-series": _create_narrow_series(np.int16), + "int32-series": _create_narrow_series(np.int32), + "uint8-series": _create_narrow_series(np.uint8), + "uint16-series": _create_narrow_series(np.uint16), + "uint32-series": _create_narrow_series(np.uint32), +} + +_all_objs = {**indices_dict, **_series, **_narrow_series} + + +@pytest.fixture(params=_all_objs.keys()) +def index_or_series_obj(request): + """ + Fixture for tests on indexes, series and series with a narrow dtype + copy to avoid mutation, e.g. setting .name + """ + return _all_objs[request.param].copy(deep=True) diff --git a/pandas/tests/base/test_ops.py b/pandas/tests/base/test_ops.py index e522c7f743a05..9deb56f070d56 100644 --- a/pandas/tests/base/test_ops.py +++ b/pandas/tests/base/test_ops.py @@ -109,26 +109,26 @@ def test_binary_ops(klass, op_name, op): assert expected_str in getattr(klass, "r" + op_name).__doc__ -class TestTranspose(Ops): +class TestTranspose: errmsg = "the 'axes' parameter is not supported" - def test_transpose(self): - for obj in self.objs: - tm.assert_equal(obj.transpose(), obj) + def test_transpose(self, index_or_series_obj): + obj = index_or_series_obj + tm.assert_equal(obj.transpose(), obj) - def test_transpose_non_default_axes(self): - for obj in self.objs: - with pytest.raises(ValueError, match=self.errmsg): - obj.transpose(1) - with pytest.raises(ValueError, match=self.errmsg): - obj.transpose(axes=1) + def test_transpose_non_default_axes(self, index_or_series_obj): + obj = index_or_series_obj + with pytest.raises(ValueError, match=self.errmsg): + obj.transpose(1) + with pytest.raises(ValueError, match=self.errmsg): + obj.transpose(axes=1) - def test_numpy_transpose(self): - for obj in self.objs: - tm.assert_equal(np.transpose(obj), obj) + def test_numpy_transpose(self, index_or_series_obj): + obj = index_or_series_obj + tm.assert_equal(np.transpose(obj), obj) - with pytest.raises(ValueError, match=self.errmsg): - np.transpose(obj, axes=1) + with pytest.raises(ValueError, match=self.errmsg): + np.transpose(obj, axes=1) class TestIndexOps(Ops): From ac4a5eb1bc71c99cfda134658e7d060e932fcb64 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Wed, 5 Feb 2020 20:11:42 +0100 Subject: [PATCH 2/4] resolved a futurewarning --- pandas/tests/base/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/base/conftest.py b/pandas/tests/base/conftest.py index 12337b3b982c6..366148e08c901 100644 --- a/pandas/tests/base/conftest.py +++ b/pandas/tests/base/conftest.py @@ -20,7 +20,7 @@ def _create_narrow_series(data_dtype): """ Helper for the _narrow_series dict """ index = indices_dict["int"].copy() size = len(index) - if np.issubdtype(data_dtype, np.float): + if np.issubdtype(data_dtype, np.floating): data = np.random.choice(size, size=size, replace=False) elif np.issubdtype(data_dtype, np.integer): data = np.random.randn(size) From 0f88d47c3158c64b74cb916bf423b8eb62527303 Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Mon, 10 Feb 2020 02:25:39 +0100 Subject: [PATCH 3/4] moved indices fixture to pandas/conftest and creating index_or_series_obj fixture there as well --- pandas/conftest.py | 74 +++++++++++++++++++++++++++++ pandas/tests/base/conftest.py | 52 -------------------- pandas/tests/indexes/conftest.py | 29 ----------- pandas/tests/indexes/test_base.py | 2 +- pandas/tests/indexes/test_setops.py | 2 +- 5 files changed, 76 insertions(+), 83 deletions(-) delete mode 100644 pandas/tests/base/conftest.py delete mode 100644 pandas/tests/indexes/conftest.py diff --git a/pandas/conftest.py b/pandas/conftest.py index 131a011c5a101..7872ecf9905e6 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -17,6 +17,7 @@ from pandas import DataFrame import pandas._testing as tm from pandas.core import ops +from pandas.core.indexes.api import Index, MultiIndex hypothesis.settings.register_profile( "ci", @@ -953,3 +954,76 @@ def __len__(self): return self._data.__len__() return TestNonDictMapping + + +indices_dict = { + "unicode": tm.makeUnicodeIndex(100), + "string": tm.makeStringIndex(100), + "datetime": tm.makeDateIndex(100), + "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), + "bool": tm.makeBoolIndex(2), + "categorical": tm.makeCategoricalIndex(100), + "interval": tm.makeIntervalIndex(100), + "empty": Index([]), + "tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])), + "repeats": Index([0, 0, 1, 1, 2, 2]), +} + + +@pytest.fixture(params=indices_dict.keys()) +def indices(request): + # copy to avoid mutation, e.g. setting .name + return indices_dict[request.param].copy() + + +def _create_series(index): + """ Helper for the _series dict """ + data = np.random.randn(len(index)) + return pd.Series(data, index=index, name=index.name) + + +_series = { + f"series-with-{id_}-index": _create_series(index) + for id_, index in indices_dict.items() +} + + +def _create_narrow_series(dtype): + """ Helper for the _narrow_series dict """ + index = indices_dict["int"].copy() + size = len(index) + if np.issubdtype(dtype, np.floating): + data = np.random.choice(size, size=size, replace=False) + elif np.issubdtype(dtype, np.integer): + data = np.random.randn(size) + else: + raise ValueError(f"Received an unexpected data_dtype: {dtype}") + return pd.Series(data.astype(dtype), index=index, name="a") + + +_narrow_series = { + "float32-series": _create_narrow_series(np.float32), + "int8-series": _create_narrow_series(np.int8), + "int16-series": _create_narrow_series(np.int16), + "int32-series": _create_narrow_series(np.int32), + "uint8-series": _create_narrow_series(np.uint8), + "uint16-series": _create_narrow_series(np.uint16), + "uint32-series": _create_narrow_series(np.uint32), +} + +_index_or_series_objs = {**indices_dict, **_series, **_narrow_series} + + +@pytest.fixture(params=_index_or_series_objs.keys()) +def index_or_series_obj(request): + """ + Fixture for tests on indexes, series and series with a narrow dtype + copy to avoid mutation, e.g. setting .name + """ + return _index_or_series_objs[request.param].copy(deep=True) diff --git a/pandas/tests/base/conftest.py b/pandas/tests/base/conftest.py deleted file mode 100644 index 366148e08c901..0000000000000 --- a/pandas/tests/base/conftest.py +++ /dev/null @@ -1,52 +0,0 @@ -import numpy as np -import pytest - -import pandas as pd -from pandas.tests.indexes.conftest import indices_dict - - -def _create_series(index): - """ Helper for the _series dict """ - data = np.random.randn(len(index)) - return pd.Series(data, index=index, name=index.name) - - -_series = { - f"series-with-{i_id}-index": _create_series(i) for i_id, i in indices_dict.items() -} - - -def _create_narrow_series(data_dtype): - """ Helper for the _narrow_series dict """ - index = indices_dict["int"].copy() - size = len(index) - if np.issubdtype(data_dtype, np.floating): - data = np.random.choice(size, size=size, replace=False) - elif np.issubdtype(data_dtype, np.integer): - data = np.random.randn(size) - else: - raise ValueError(f"Received an unexpected data_dtype: {data_dtype}") - - return pd.Series(data.astype(data_dtype), index=index, name="a") - - -_narrow_series = { - "float32-series": _create_narrow_series(np.float32), - "int8-series": _create_narrow_series(np.int8), - "int16-series": _create_narrow_series(np.int16), - "int32-series": _create_narrow_series(np.int32), - "uint8-series": _create_narrow_series(np.uint8), - "uint16-series": _create_narrow_series(np.uint16), - "uint32-series": _create_narrow_series(np.uint32), -} - -_all_objs = {**indices_dict, **_series, **_narrow_series} - - -@pytest.fixture(params=_all_objs.keys()) -def index_or_series_obj(request): - """ - Fixture for tests on indexes, series and series with a narrow dtype - copy to avoid mutation, e.g. setting .name - """ - return _all_objs[request.param].copy(deep=True) diff --git a/pandas/tests/indexes/conftest.py b/pandas/tests/indexes/conftest.py deleted file mode 100644 index 57174f206b70d..0000000000000 --- a/pandas/tests/indexes/conftest.py +++ /dev/null @@ -1,29 +0,0 @@ -import pytest - -import pandas._testing as tm -from pandas.core.indexes.api import Index, MultiIndex - -indices_dict = { - "unicode": tm.makeUnicodeIndex(100), - "string": tm.makeStringIndex(100), - "datetime": tm.makeDateIndex(100), - "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), - "bool": tm.makeBoolIndex(2), - "categorical": tm.makeCategoricalIndex(100), - "interval": tm.makeIntervalIndex(100), - "empty": Index([]), - "tuples": MultiIndex.from_tuples(zip(["foo", "bar", "baz"], [1, 2, 3])), - "repeats": Index([0, 0, 1, 1, 2, 2]), -} - - -@pytest.fixture(params=indices_dict.keys()) -def indices(request): - # copy to avoid mutation, e.g. setting .name - return indices_dict[request.param].copy() diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 04af9b09bbf89..c64a70af6f2a4 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -34,6 +34,7 @@ period_range, ) import pandas._testing as tm +from pandas.conftest import indices_dict from pandas.core.indexes.api import ( Index, MultiIndex, @@ -42,7 +43,6 @@ ensure_index_from_sequences, ) from pandas.tests.indexes.common import Base -from pandas.tests.indexes.conftest import indices_dict class TestIndex(Base): diff --git a/pandas/tests/indexes/test_setops.py b/pandas/tests/indexes/test_setops.py index abfa413d56655..d0cbb2ab75f72 100644 --- a/pandas/tests/indexes/test_setops.py +++ b/pandas/tests/indexes/test_setops.py @@ -13,7 +13,7 @@ from pandas import Float64Index, Int64Index, RangeIndex, UInt64Index import pandas._testing as tm from pandas.api.types import pandas_dtype -from pandas.tests.indexes.conftest import indices_dict +from pandas.conftest import indices_dict COMPATIBLE_INCONSISTENT_PAIRS = { (Int64Index, RangeIndex): (tm.makeIntIndex, tm.makeRangeIndex), From e865e9056d19834be404283f8f539111db3f6e9e Mon Sep 17 00:00:00 2001 From: Martin Winkel Date: Thu, 13 Feb 2020 00:44:19 +0100 Subject: [PATCH 4/4] minor simplifications of _narrow_series creation in pandas/conftest.py --- pandas/conftest.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 7872ecf9905e6..78feaa817b531 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -984,37 +984,30 @@ def indices(request): def _create_series(index): """ Helper for the _series dict """ - data = np.random.randn(len(index)) - return pd.Series(data, index=index, name=index.name) + size = len(index) + data = np.random.randn(size) + return pd.Series(data, index=index, name="a") _series = { - f"series-with-{id_}-index": _create_series(index) - for id_, index in indices_dict.items() + f"series-with-{index_id}-index": _create_series(index) + for index_id, index in indices_dict.items() } -def _create_narrow_series(dtype): - """ Helper for the _narrow_series dict """ - index = indices_dict["int"].copy() - size = len(index) - if np.issubdtype(dtype, np.floating): - data = np.random.choice(size, size=size, replace=False) - elif np.issubdtype(dtype, np.integer): - data = np.random.randn(size) - else: - raise ValueError(f"Received an unexpected data_dtype: {dtype}") - return pd.Series(data.astype(dtype), index=index, name="a") - - +_narrow_dtypes = [ + np.float16, + np.float32, + np.int8, + np.int16, + np.int32, + np.uint8, + np.uint16, + np.uint32, +] _narrow_series = { - "float32-series": _create_narrow_series(np.float32), - "int8-series": _create_narrow_series(np.int8), - "int16-series": _create_narrow_series(np.int16), - "int32-series": _create_narrow_series(np.int32), - "uint8-series": _create_narrow_series(np.uint8), - "uint16-series": _create_narrow_series(np.uint16), - "uint32-series": _create_narrow_series(np.uint32), + f"{dtype.__name__}-series": tm.makeFloatSeries(name="a").astype(dtype) + for dtype in _narrow_dtypes } _index_or_series_objs = {**indices_dict, **_series, **_narrow_series}