Skip to content

Commit b2df169

Browse files
committed
TST: clean up series/frame api tests inheritance a bit
1 parent 751119f commit b2df169

File tree

6 files changed

+218
-196
lines changed

6 files changed

+218
-196
lines changed

pandas/tests/frame/common.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,49 @@ def simple(self):
104104
return pd.DataFrame(arr, columns=['one', 'two', 'three'],
105105
index=['a', 'b', 'c'])
106106

107+
108+
class SharedWithSparse(TestData):
109+
110+
def test_copy_index_name_checking(self):
111+
# don't want to be able to modify the index stored elsewhere after
112+
# making a copy
113+
for attr in ('index', 'columns'):
114+
ind = getattr(self.frame, attr)
115+
ind.name = None
116+
cp = self.frame.copy()
117+
getattr(cp, attr).name = 'foo'
118+
self.assertIsNone(getattr(self.frame, attr).name)
119+
120+
def test_getitem_pop_assign_name(self):
121+
s = self.frame['A']
122+
self.assertEqual(s.name, 'A')
123+
124+
s = self.frame.pop('A')
125+
self.assertEqual(s.name, 'A')
126+
127+
s = self.frame.loc[:, 'B']
128+
self.assertEqual(s.name, 'B')
129+
130+
s2 = s.loc[:]
131+
self.assertEqual(s2.name, 'B')
132+
133+
def test_get_value(self):
134+
for idx in self.frame.index:
135+
for col in self.frame.columns:
136+
result = self.frame.get_value(idx, col)
137+
expected = self.frame[col][idx]
138+
tm.assert_almost_equal(result, expected)
139+
140+
def test_add_prefix_suffix(self):
141+
with_prefix = self.frame.add_prefix('foo#')
142+
expected = pd.Index(['foo#%s' % c for c in self.frame.columns])
143+
self.assert_index_equal(with_prefix.columns, expected)
144+
145+
with_suffix = self.frame.add_suffix('#foo')
146+
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
147+
self.assert_index_equal(with_suffix.columns, expected)
148+
149+
107150
# self.ts3 = tm.makeTimeSeries()[-5:]
108151
# self.ts4 = tm.makeTimeSeries()[1:-1]
109152

pandas/tests/frame/test_misc_api.py renamed to pandas/tests/frame/test_api.py

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,10 @@
2222

2323
import pandas.util.testing as tm
2424

25-
from pandas.tests.frame.common import TestData
25+
from pandas.tests.frame.common import SharedWithSparse
2626

2727

28-
class SharedWithSparse(object):
29-
30-
def test_copy_index_name_checking(self):
31-
# don't want to be able to modify the index stored elsewhere after
32-
# making a copy
33-
for attr in ('index', 'columns'):
34-
ind = getattr(self.frame, attr)
35-
ind.name = None
36-
cp = self.frame.copy()
37-
getattr(cp, attr).name = 'foo'
38-
self.assertIsNone(getattr(self.frame, attr).name)
39-
40-
def test_getitem_pop_assign_name(self):
41-
s = self.frame['A']
42-
self.assertEqual(s.name, 'A')
43-
44-
s = self.frame.pop('A')
45-
self.assertEqual(s.name, 'A')
46-
47-
s = self.frame.loc[:, 'B']
48-
self.assertEqual(s.name, 'B')
49-
50-
s2 = s.loc[:]
51-
self.assertEqual(s2.name, 'B')
52-
53-
def test_get_value(self):
54-
for idx in self.frame.index:
55-
for col in self.frame.columns:
56-
result = self.frame.get_value(idx, col)
57-
expected = self.frame[col][idx]
58-
tm.assert_almost_equal(result, expected)
59-
60-
def test_add_prefix_suffix(self):
61-
with_prefix = self.frame.add_prefix('foo#')
62-
expected = pd.Index(['foo#%s' % c for c in self.frame.columns])
63-
self.assert_index_equal(with_prefix.columns, expected)
64-
65-
with_suffix = self.frame.add_suffix('#foo')
66-
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
67-
self.assert_index_equal(with_suffix.columns, expected)
68-
69-
70-
class TestDataFrameMisc(tm.TestCase, SharedWithSparse, TestData):
28+
class TestDataFrameMisc(tm.TestCase, SharedWithSparse):
7129

7230
klass = DataFrame
7331

pandas/tests/series/common.py

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
""" common series tests routines """
12
from pandas.util.decorators import cache_readonly
3+
24
import pandas.util.testing as tm
35
import pandas as pd
4-
5-
_ts = tm.makeTimeSeries()
6+
import pandas.formats.printing as printing
67

78

89
class TestData(object):
910

1011
@cache_readonly
1112
def ts(self):
12-
ts = _ts.copy()
13+
ts = tm.makeTimeSeries()
1314
ts.name = 'ts'
1415
return ts
1516

@@ -28,3 +29,103 @@ def objSeries(self):
2829
@cache_readonly
2930
def empty(self):
3031
return pd.Series([], index=[])
32+
33+
34+
class SharedWithSparse(TestData):
35+
36+
def test_scalarop_preserve_name(self):
37+
result = self.ts * 2
38+
assert result.name == self.ts.name
39+
40+
def test_copy_name(self):
41+
result = self.ts.copy()
42+
assert result.name == self.ts.name
43+
44+
def test_copy_index_name_checking(self):
45+
# don't want to be able to modify the index stored elsewhere after
46+
# making a copy
47+
48+
self.ts.index.name = None
49+
assert self.ts.index.name is None
50+
assert self.ts is self.ts
51+
52+
cp = self.ts.copy()
53+
cp.index.name = 'foo'
54+
printing.pprint_thing(self.ts.index.name)
55+
assert self.ts.index.name is None
56+
57+
def test_append_preserve_name(self):
58+
result = self.ts[:5].append(self.ts[5:])
59+
assert result.name == self.ts.name
60+
61+
def test_binop_maybe_preserve_name(self):
62+
# names match, preserve
63+
result = self.ts * self.ts
64+
assert result.name == self.ts.name
65+
result = self.ts.mul(self.ts)
66+
assert result.name == self.ts.name
67+
68+
result = self.ts * self.ts[:-2]
69+
assert result.name == self.ts.name
70+
71+
# names don't match, don't preserve
72+
cp = self.ts.copy()
73+
cp.name = 'something else'
74+
result = self.ts + cp
75+
assert result.name is None
76+
77+
result = self.ts.add(cp)
78+
assert result.name is None
79+
80+
ops = ['add', 'sub', 'mul', 'div', 'truediv', 'floordiv', 'mod', 'pow']
81+
ops = ops + ['r' + op for op in ops]
82+
for op in ops:
83+
# names match, preserve
84+
s = self.ts.copy()
85+
result = getattr(s, op)(s)
86+
assert result.name == self.ts.name
87+
88+
# names don't match, don't preserve
89+
cp = self.ts.copy()
90+
cp.name = 'changed'
91+
result = getattr(s, op)(cp)
92+
assert result.name is None
93+
94+
def test_combine_first_name(self):
95+
result = self.ts.combine_first(self.ts[:5])
96+
assert result.name == self.ts.name
97+
98+
def test_getitem_preserve_name(self):
99+
result = self.ts[self.ts > 0]
100+
assert result.name == self.ts.name
101+
102+
result = self.ts[[0, 2, 4]]
103+
assert result.name == self.ts.name
104+
105+
result = self.ts[5:10]
106+
assert result.name == self.ts.name
107+
108+
def test_pickle(self):
109+
unp_series = self._pickle_roundtrip(self.series)
110+
unp_ts = self._pickle_roundtrip(self.ts)
111+
tm.assert_series_equal(unp_series, self.series)
112+
tm.assert_series_equal(unp_ts, self.ts)
113+
114+
def _pickle_roundtrip(self, obj):
115+
116+
with tm.ensure_clean() as path:
117+
obj.to_pickle(path)
118+
unpickled = pd.read_pickle(path)
119+
return unpickled
120+
121+
def test_argsort_preserve_name(self):
122+
result = self.ts.argsort()
123+
assert result.name == self.ts.name
124+
125+
def test_sort_index_name(self):
126+
result = self.ts.sort_index(ascending=False)
127+
assert result.name == self.ts.name
128+
129+
def test_to_sparse_pass_name(self):
130+
result = self.ts.to_sparse()
131+
assert result.name == self.ts.name

0 commit comments

Comments
 (0)