Skip to content

Commit bd4e246

Browse files
committed
CLN: Fixturize test_base.py
Fixes #23877
1 parent 4b6be69 commit bd4e246

File tree

3 files changed

+163
-94
lines changed

3 files changed

+163
-94
lines changed

pandas/tests/conftest.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numpy as np
2+
import pytest
3+
4+
from pandas import Series
5+
import pandas.util.testing as tm
6+
7+
_all_indices = [tm.makeBoolIndex(10, name='a'),
8+
tm.makeIntIndex(10, name='a'),
9+
tm.makeFloatIndex(10, name='a'),
10+
tm.makeDateIndex(10, name='a'),
11+
tm.makeDateIndex(10, name='a').tz_localize(tz='US/Eastern'),
12+
tm.makePeriodIndex(10, name='a'),
13+
tm.makeStringIndex(10, name='a'),
14+
tm.makeUnicodeIndex(10, name='a')]
15+
16+
_all_indices_ids = [
17+
'testing boolean index',
18+
'testing integer index',
19+
'testing float index',
20+
'testing date index',
21+
'testing date index with timezone',
22+
'testing period index',
23+
'testing string index',
24+
'testing unicode index',
25+
]
26+
27+
_arr = np.arange(10)
28+
29+
_all_series = [Series(_arr, index=tm.makeBoolIndex(10, name='a')),
30+
Series(_arr, index=tm.makeIntIndex(10, name='a')),
31+
Series(_arr, index=tm.makeFloatIndex(10, name='a')),
32+
Series(_arr, index=tm.makeDateIndex(10, name='a')),
33+
tm.makeDateIndex(10, name='a').tz_localize(
34+
tz='US/Eastern').to_series(keep_tz=True),
35+
Series(_arr, index=tm.makePeriodIndex(10, name='a')),
36+
Series(_arr, index=tm.makeStringIndex(10, name='a')),
37+
Series(_arr, index=tm.makeUnicodeIndex(10, name='a'))]
38+
39+
_all_series_ids = [
40+
'testing pandas series with boolean index',
41+
'testing pandas series with integer index',
42+
'testing pandas series with float index',
43+
'testing pandas series with date index',
44+
'testing pandas series with date index with timezone',
45+
'testing pandas series with period index',
46+
'testing pandas series with string index',
47+
'testing pandas series with unicode index',
48+
]
49+
50+
51+
@pytest.fixture(params=_all_indices, ids=_all_indices_ids)
52+
def all_indices_fixture(request):
53+
"""Fixture for testing index operations"""
54+
return request.param
55+
56+
57+
@pytest.fixture(params=_all_series, ids=_all_series_ids)
58+
def all_series_fixture(request):
59+
"""Fixture for testing pandas Series operations"""
60+
return request.param
61+
62+
63+
@pytest.fixture(params=_all_indices + _all_series,
64+
ids=_all_indices_ids + _all_series_ids)
65+
def all_indices_and_series_fixture(request):
66+
"""Fixture for testing index and pandas Series operations"""
67+
return request.param

pandas/tests/test_base.py

Lines changed: 88 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -263,77 +263,72 @@ def setup_method(self, method):
263263
self.is_valid_objs = self.objs
264264
self.not_valid_objs = []
265265

266-
def test_none_comparison(self):
267-
266+
def test_none_comparison(self, all_series_fixture):
268267
# bug brought up by #1079
269268
# changed from TypeError in 0.17.0
270-
for o in self.is_valid_objs:
271-
if isinstance(o, Series):
269+
s = all_series_fixture
270+
s[0] = np.nan
271+
272+
# noinspection PyComparisonWithNone
273+
result = s == None # noqa
274+
assert not result.iat[0]
275+
assert not result.iat[1]
276+
277+
# noinspection PyComparisonWithNone
278+
result = s != None # noqa
279+
assert result.iat[0]
280+
assert result.iat[1]
281+
282+
result = None == s # noqa
283+
assert not result.iat[0]
284+
assert not result.iat[1]
285+
286+
result = None != s # noqa
287+
assert result.iat[0]
288+
assert result.iat[1]
289+
290+
if (is_datetime64_dtype(s) or
291+
is_datetime64tz_dtype(s)):
292+
# Following DatetimeIndex (and Timestamp) convention,
293+
# inequality comparisons with Series[datetime64] raise
294+
with pytest.raises(TypeError):
295+
None > s
296+
with pytest.raises(TypeError):
297+
s > None
298+
else:
299+
result = None > s
300+
assert not result.iat[0]
301+
assert not result.iat[1]
272302

273-
o[0] = np.nan
274-
275-
# noinspection PyComparisonWithNone
276-
result = o == None # noqa
277-
assert not result.iat[0]
278-
assert not result.iat[1]
279-
280-
# noinspection PyComparisonWithNone
281-
result = o != None # noqa
282-
assert result.iat[0]
283-
assert result.iat[1]
284-
285-
result = None == o # noqa
286-
assert not result.iat[0]
287-
assert not result.iat[1]
288-
289-
result = None != o # noqa
290-
assert result.iat[0]
291-
assert result.iat[1]
292-
293-
if (is_datetime64_dtype(o) or is_datetime64tz_dtype(o)):
294-
# Following DatetimeIndex (and Timestamp) convention,
295-
# inequality comparisons with Series[datetime64] raise
296-
with pytest.raises(TypeError):
297-
None > o
298-
with pytest.raises(TypeError):
299-
o > None
300-
else:
301-
result = None > o
302-
assert not result.iat[0]
303-
assert not result.iat[1]
303+
result = s < None
304+
assert not result.iat[0]
305+
assert not result.iat[1]
304306

305-
result = o < None
306-
assert not result.iat[0]
307-
assert not result.iat[1]
307+
def test_ndarray_compat_properties(self, all_indices_and_series_fixture):
308308

309-
def test_ndarray_compat_properties(self):
309+
o = all_indices_and_series_fixture
310+
# Check that we work.
311+
for p in ['shape', 'dtype', 'T', 'nbytes']:
312+
assert getattr(o, p, None) is not None
310313

311-
for o in self.objs:
312-
# Check that we work.
313-
for p in ['shape', 'dtype', 'T', 'nbytes']:
314+
# deprecated properties
315+
for p in ['flags', 'strides', 'itemsize']:
316+
with tm.assert_produces_warning(FutureWarning):
314317
assert getattr(o, p, None) is not None
315318

316-
# deprecated properties
317-
for p in ['flags', 'strides', 'itemsize']:
318-
with tm.assert_produces_warning(FutureWarning):
319-
assert getattr(o, p, None) is not None
319+
with tm.assert_produces_warning(FutureWarning):
320+
assert hasattr(o, 'base')
320321

321-
with tm.assert_produces_warning(FutureWarning):
322-
assert hasattr(o, 'base')
323-
324-
# If we have a datetime-like dtype then needs a view to work
325-
# but the user is responsible for that
326-
try:
327-
with tm.assert_produces_warning(FutureWarning):
328-
assert o.data is not None
329-
except ValueError:
330-
pass
322+
# If we have a datetime-like dtype then needs a view to work
323+
# but the user is responsible for that
324+
with tm.assert_produces_warning(FutureWarning):
325+
assert o.data is not None
331326

332-
with pytest.raises(ValueError):
333-
o.item() # len > 1
327+
with pytest.raises(ValueError):
328+
o.item() # len > 1
334329

335-
assert o.ndim == 1
336-
assert o.size == len(o)
330+
assert o.ndim == 1
331+
assert o.size == len(o)
337332

338333
assert Index([1]).item() == 1
339334
assert Series([1]).item() == 1
@@ -928,54 +923,53 @@ def test_validate_bool_args(self):
928923
with pytest.raises(ValueError):
929924
self.int_series.drop_duplicates(inplace=value)
930925

931-
def test_getitem(self):
932-
for i in self.indexes:
933-
s = pd.Series(i)
926+
def test_getitem(self, all_indices_fixture):
927+
idx = all_indices_fixture
928+
s = pd.Series(idx)
934929

935-
assert i[0] == s.iloc[0]
936-
assert i[5] == s.iloc[5]
937-
assert i[-1] == s.iloc[-1]
930+
assert idx[0] == s.iloc[0]
931+
assert idx[5] == s.iloc[5]
932+
assert idx[-1] == s.iloc[-1]
938933

939-
assert i[-1] == i[9]
934+
assert idx[-1] == idx[9]
940935

941-
with pytest.raises(IndexError):
942-
i[20]
943-
with pytest.raises(IndexError):
944-
s.iloc[20]
936+
with pytest.raises(IndexError):
937+
idx[20]
938+
with pytest.raises(IndexError):
939+
s.iloc[20]
945940

946941
@pytest.mark.parametrize('indexer_klass', [list, pd.Index])
947942
@pytest.mark.parametrize('indexer', [[True] * 10, [False] * 10,
948943
[True, False, True, True, False,
949944
False, True, True, False, True]])
950-
def test_bool_indexing(self, indexer_klass, indexer):
945+
def test_bool_indexing(self, indexer_klass, indexer, all_indices_fixture):
951946
# GH 22533
952-
for idx in self.indexes:
953-
exp_idx = [i for i in range(len(indexer)) if indexer[i]]
954-
tm.assert_index_equal(idx[indexer_klass(indexer)], idx[exp_idx])
955-
s = pd.Series(idx)
956-
tm.assert_series_equal(s[indexer_klass(indexer)], s.iloc[exp_idx])
947+
idx = all_indices_fixture
948+
exp_idx = [i for i in range(len(indexer)) if indexer[i]]
949+
tm.assert_index_equal(idx[indexer_klass(indexer)], idx[exp_idx])
950+
s = pd.Series(idx)
951+
tm.assert_series_equal(s[indexer_klass(indexer)], s.iloc[exp_idx])
957952

958953

959954
class TestTranspose(Ops):
960955
errmsg = "the 'axes' parameter is not supported"
961956

962-
def test_transpose(self):
963-
for obj in self.objs:
964-
tm.assert_equal(obj.transpose(), obj)
965-
966-
def test_transpose_non_default_axes(self):
967-
for obj in self.objs:
968-
with pytest.raises(ValueError, match=self.errmsg):
969-
obj.transpose(1)
970-
with pytest.raises(ValueError, match=self.errmsg):
971-
obj.transpose(axes=1)
972-
973-
def test_numpy_transpose(self):
974-
for obj in self.objs:
975-
tm.assert_equal(np.transpose(obj), obj)
976-
977-
with pytest.raises(ValueError, match=self.errmsg):
978-
np.transpose(obj, axes=1)
957+
def test_transpose(self, all_indices_and_series_fixture):
958+
obj = all_indices_and_series_fixture
959+
tm.assert_equal(obj.transpose(), obj)
960+
961+
def test_transpose_non_default_axes(self, all_indices_and_series_fixture):
962+
obj = all_indices_and_series_fixture
963+
with pytest.raises(ValueError, match=self.errmsg):
964+
obj.transpose(1)
965+
with pytest.raises(ValueError, match=self.errmsg):
966+
obj.transpose(axes=1)
967+
968+
def test_numpy_transpose(self, all_indices_and_series_fixture):
969+
obj = all_indices_and_series_fixture
970+
tm.assert_equal(np.transpose(obj), obj)
971+
with pytest.raises(ValueError, match=self.errmsg):
972+
np.transpose(obj, axes=1)
979973

980974

981975
class TestNoNewAttributesMixin(object):

pandas/util/testing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,14 @@ def getArangeMat():
18591859

18601860

18611861
# make index
1862+
def makeEmptyIndex(name=None):
1863+
return Index([], name=name)
1864+
1865+
1866+
def makeRepeatIndex(name=None):
1867+
return Index([0, 0, 1, 1, 2, 2], name=name)
1868+
1869+
18621870
def makeStringIndex(k=10, name=None):
18631871
return Index(rands_array(nchars=10, size=k), name=name)
18641872

0 commit comments

Comments
 (0)