|
10 | 10 |
|
11 | 11 | from pandas.compat import range
|
12 | 12 | from pandas import compat
|
13 |
| -from pandas.util.testing import assert_series_equal |
| 13 | +from pandas.util.testing import (assert_series_equal, |
| 14 | + ensure_clean) |
14 | 15 | import pandas.util.testing as tm
|
15 | 16 |
|
16 | 17 | from .common import TestData
|
17 | 18 |
|
18 | 19 |
|
19 |
| -class TestSeriesMisc(TestData, tm.TestCase): |
| 20 | +class SharedWithSparse(object): |
| 21 | + |
| 22 | + def test_scalarop_preserve_name(self): |
| 23 | + result = self.ts * 2 |
| 24 | + self.assertEqual(result.name, self.ts.name) |
| 25 | + |
| 26 | + def test_copy_name(self): |
| 27 | + result = self.ts.copy() |
| 28 | + self.assertEqual(result.name, self.ts.name) |
| 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 | + |
| 34 | + self.ts.index.name = None |
| 35 | + self.assertIsNone(self.ts.index.name) |
| 36 | + self.assertIs(self.ts, self.ts) |
| 37 | + |
| 38 | + cp = self.ts.copy() |
| 39 | + cp.index.name = 'foo' |
| 40 | + com.pprint_thing(self.ts.index.name) |
| 41 | + self.assertIsNone(self.ts.index.name) |
| 42 | + |
| 43 | + def test_append_preserve_name(self): |
| 44 | + result = self.ts[:5].append(self.ts[5:]) |
| 45 | + self.assertEqual(result.name, self.ts.name) |
| 46 | + |
| 47 | + def test_binop_maybe_preserve_name(self): |
| 48 | + # names match, preserve |
| 49 | + result = self.ts * self.ts |
| 50 | + self.assertEqual(result.name, self.ts.name) |
| 51 | + result = self.ts.mul(self.ts) |
| 52 | + self.assertEqual(result.name, self.ts.name) |
| 53 | + |
| 54 | + result = self.ts * self.ts[:-2] |
| 55 | + self.assertEqual(result.name, self.ts.name) |
| 56 | + |
| 57 | + # names don't match, don't preserve |
| 58 | + cp = self.ts.copy() |
| 59 | + cp.name = 'something else' |
| 60 | + result = self.ts + cp |
| 61 | + self.assertIsNone(result.name) |
| 62 | + result = self.ts.add(cp) |
| 63 | + self.assertIsNone(result.name) |
| 64 | + |
| 65 | + ops = ['add', 'sub', 'mul', 'div', 'truediv', 'floordiv', 'mod', 'pow'] |
| 66 | + ops = ops + ['r' + op for op in ops] |
| 67 | + for op in ops: |
| 68 | + # names match, preserve |
| 69 | + s = self.ts.copy() |
| 70 | + result = getattr(s, op)(s) |
| 71 | + self.assertEqual(result.name, self.ts.name) |
| 72 | + |
| 73 | + # names don't match, don't preserve |
| 74 | + cp = self.ts.copy() |
| 75 | + cp.name = 'changed' |
| 76 | + result = getattr(s, op)(cp) |
| 77 | + self.assertIsNone(result.name) |
| 78 | + |
| 79 | + def test_combine_first_name(self): |
| 80 | + result = self.ts.combine_first(self.ts[:5]) |
| 81 | + self.assertEqual(result.name, self.ts.name) |
| 82 | + |
| 83 | + def test_getitem_preserve_name(self): |
| 84 | + result = self.ts[self.ts > 0] |
| 85 | + self.assertEqual(result.name, self.ts.name) |
| 86 | + |
| 87 | + result = self.ts[[0, 2, 4]] |
| 88 | + self.assertEqual(result.name, self.ts.name) |
| 89 | + |
| 90 | + result = self.ts[5:10] |
| 91 | + self.assertEqual(result.name, self.ts.name) |
| 92 | + |
| 93 | + def test_pickle(self): |
| 94 | + unp_series = self._pickle_roundtrip(self.series) |
| 95 | + unp_ts = self._pickle_roundtrip(self.ts) |
| 96 | + assert_series_equal(unp_series, self.series) |
| 97 | + assert_series_equal(unp_ts, self.ts) |
| 98 | + |
| 99 | + def _pickle_roundtrip(self, obj): |
| 100 | + |
| 101 | + with ensure_clean() as path: |
| 102 | + obj.to_pickle(path) |
| 103 | + unpickled = pd.read_pickle(path) |
| 104 | + return unpickled |
| 105 | + |
| 106 | + def test_argsort_preserve_name(self): |
| 107 | + result = self.ts.argsort() |
| 108 | + self.assertEqual(result.name, self.ts.name) |
| 109 | + |
| 110 | + def test_sort_index_name(self): |
| 111 | + result = self.ts.sort_index(ascending=False) |
| 112 | + self.assertEqual(result.name, self.ts.name) |
| 113 | + |
| 114 | + def test_to_sparse_pass_name(self): |
| 115 | + result = self.ts.to_sparse() |
| 116 | + self.assertEqual(result.name, self.ts.name) |
| 117 | + |
| 118 | + |
| 119 | +class TestSeriesMisc(TestData, SharedWithSparse, tm.TestCase): |
20 | 120 |
|
21 | 121 | _multiprocess_can_split_ = True
|
22 | 122 |
|
@@ -144,27 +244,6 @@ def test_numpy_unique(self):
|
144 | 244 | # it works!
|
145 | 245 | np.unique(self.ts)
|
146 | 246 |
|
147 |
| - def test_copy_name(self): |
148 |
| - result = self.ts.copy() |
149 |
| - self.assertEqual(result.name, self.ts.name) |
150 |
| - |
151 |
| - def test_copy_index_name_checking(self): |
152 |
| - # don't want to be able to modify the index stored elsewhere after |
153 |
| - # making a copy |
154 |
| - |
155 |
| - self.ts.index.name = None |
156 |
| - self.assertIsNone(self.ts.index.name) |
157 |
| - self.assertIs(self.ts, self.ts) |
158 |
| - |
159 |
| - cp = self.ts.copy() |
160 |
| - cp.index.name = 'foo' |
161 |
| - com.pprint_thing(self.ts.index.name) |
162 |
| - self.assertIsNone(self.ts.index.name) |
163 |
| - |
164 |
| - def test_to_sparse_pass_name(self): |
165 |
| - result = self.ts.to_sparse() |
166 |
| - self.assertEqual(result.name, self.ts.name) |
167 |
| - |
168 | 247 | def test_ndarray_compat(self):
|
169 | 248 |
|
170 | 249 | # test numpy compat with Series as sub-class of NDFrame
|
|
0 commit comments