Skip to content

Commit 3a9802d

Browse files
started to fixturize pandas/tests/base
1 parent 2f9a446 commit 3a9802d

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

pandas/tests/base/conftest.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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)

pandas/tests/base/test_ops.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,26 @@ def test_binary_ops(klass, op_name, op):
109109
assert expected_str in getattr(klass, "r" + op_name).__doc__
110110

111111

112-
class TestTranspose(Ops):
112+
class TestTranspose:
113113
errmsg = "the 'axes' parameter is not supported"
114114

115-
def test_transpose(self):
116-
for obj in self.objs:
117-
tm.assert_equal(obj.transpose(), obj)
115+
def test_transpose(self, index_or_series_obj):
116+
obj = index_or_series_obj
117+
tm.assert_equal(obj.transpose(), obj)
118118

119-
def test_transpose_non_default_axes(self):
120-
for obj in self.objs:
121-
with pytest.raises(ValueError, match=self.errmsg):
122-
obj.transpose(1)
123-
with pytest.raises(ValueError, match=self.errmsg):
124-
obj.transpose(axes=1)
119+
def test_transpose_non_default_axes(self, index_or_series_obj):
120+
obj = index_or_series_obj
121+
with pytest.raises(ValueError, match=self.errmsg):
122+
obj.transpose(1)
123+
with pytest.raises(ValueError, match=self.errmsg):
124+
obj.transpose(axes=1)
125125

126-
def test_numpy_transpose(self):
127-
for obj in self.objs:
128-
tm.assert_equal(np.transpose(obj), obj)
126+
def test_numpy_transpose(self, index_or_series_obj):
127+
obj = index_or_series_obj
128+
tm.assert_equal(np.transpose(obj), obj)
129129

130-
with pytest.raises(ValueError, match=self.errmsg):
131-
np.transpose(obj, axes=1)
130+
with pytest.raises(ValueError, match=self.errmsg):
131+
np.transpose(obj, axes=1)
132132

133133

134134
class TestIndexOps(Ops):

0 commit comments

Comments
 (0)