|
1 | 1 | import re
|
2 | 2 | import numpy as np
|
3 | 3 | import pandas.compat as compat
|
| 4 | +import pandas as pd |
4 | 5 | from pandas.compat import u
|
5 | 6 | from pandas.core.base import FrozenList, FrozenNDArray
|
6 | 7 | from pandas.util.testing import assertRaisesRegexp, assert_isinstance
|
| 8 | +from pandas import Series, Index, DatetimeIndex, PeriodIndex |
| 9 | +from pandas import _np_version_under1p7 |
| 10 | +import nose |
| 11 | + |
7 | 12 | import pandas.util.testing as tm
|
8 | 13 |
|
9 | 14 | class CheckStringMixin(object):
|
@@ -120,6 +125,101 @@ def test_values(self):
|
120 | 125 | self.assert_numpy_array_equal(self.container, original)
|
121 | 126 | self.assertEqual(vals[0], n)
|
122 | 127 |
|
| 128 | +class Ops(tm.TestCase): |
| 129 | + def setUp(self): |
| 130 | + self.int_index = tm.makeIntIndex(10) |
| 131 | + self.float_index = tm.makeFloatIndex(10) |
| 132 | + self.dt_index = tm.makeDateIndex(10) |
| 133 | + self.period_index = tm.makePeriodIndex(10) |
| 134 | + self.string_index = tm.makeStringIndex(10) |
| 135 | + |
| 136 | + arr = np.random.randn(10) |
| 137 | + self.int_series = Series(arr, index=self.int_index) |
| 138 | + self.float_series = Series(arr, index=self.int_index) |
| 139 | + self.dt_series = Series(arr, index=self.dt_index) |
| 140 | + self.period_series = Series(arr, index=self.period_index) |
| 141 | + self.string_series = Series(arr, index=self.string_index) |
| 142 | + |
| 143 | + self.objs = [ getattr(self,"{0}_{1}".format(t,f)) for t in ['int','float','dt','period','string'] for f in ['index','series'] ] |
| 144 | + |
| 145 | + def check_ops_properties(self, props, filter=None, ignore_failures=False): |
| 146 | + for op in props: |
| 147 | + for o in self.is_valid_objs: |
| 148 | + |
| 149 | + # if a filter, skip if it doesn't match |
| 150 | + if filter is not None: |
| 151 | + filt = o.index if isinstance(o, Series) else o |
| 152 | + if not filter(filt): |
| 153 | + continue |
| 154 | + |
| 155 | + try: |
| 156 | + if isinstance(o, Series): |
| 157 | + expected = Series(getattr(o.index,op),index=o.index) |
| 158 | + else: |
| 159 | + expected = getattr(o,op) |
| 160 | + except (AttributeError): |
| 161 | + if ignore_failures: |
| 162 | + continue |
| 163 | + |
| 164 | + result = getattr(o,op) |
| 165 | + |
| 166 | + # these couuld be series, arrays or scalars |
| 167 | + if isinstance(result,Series) and isinstance(expected,Series): |
| 168 | + tm.assert_series_equal(result,expected) |
| 169 | + elif isinstance(result,Index) and isinstance(expected,Index): |
| 170 | + tm.assert_index_equal(result,expected) |
| 171 | + elif isinstance(result,np.ndarray) and isinstance(expected,np.ndarray): |
| 172 | + self.assert_numpy_array_equal(result,expected) |
| 173 | + else: |
| 174 | + self.assertEqual(result, expected) |
| 175 | + |
| 176 | + # freq raises AttributeError on an Int64Index because its not defined |
| 177 | + # we mostly care about Series hwere anyhow |
| 178 | + if not ignore_failures: |
| 179 | + for o in self.not_valid_objs: |
| 180 | + self.assertRaises(TypeError, lambda : getattr(o,op)) |
| 181 | + |
| 182 | +class TestIndexOps(Ops): |
| 183 | + |
| 184 | + def setUp(self): |
| 185 | + super(TestIndexOps, self).setUp() |
| 186 | + self.is_valid_objs = [ o for o in self.objs if o._allow_index_ops ] |
| 187 | + self.not_valid_objs = [ o for o in self.objs if not o._allow_index_ops ] |
| 188 | + |
| 189 | + def test_ops(self): |
| 190 | + if _np_version_under1p7: |
| 191 | + raise nose.SkipTest("test only valid in numpy >= 1.7") |
| 192 | + for op in ['max','min']: |
| 193 | + for o in self.objs: |
| 194 | + result = getattr(o,op)() |
| 195 | + expected = getattr(o.values,op)() |
| 196 | + self.assertEqual(result, expected) |
| 197 | + |
| 198 | +class TestDatetimeIndexOps(Ops): |
| 199 | + _allowed = '_allow_datetime_index_ops' |
| 200 | + |
| 201 | + def setUp(self): |
| 202 | + super(TestDatetimeIndexOps, self).setUp() |
| 203 | + mask = lambda x: x._allow_datetime_index_ops or x._allow_period_index_ops |
| 204 | + self.is_valid_objs = [ o for o in self.objs if mask(o) ] |
| 205 | + self.not_valid_objs = [ o for o in self.objs if not mask(o) ] |
| 206 | + |
| 207 | + def test_ops_properties(self): |
| 208 | + self.check_ops_properties(['year','month','day','hour','minute','second','weekofyear','week','dayofweek','dayofyear','quarter']) |
| 209 | + self.check_ops_properties(['date','time','microsecond','nanosecond'], lambda x: isinstance(x,DatetimeIndex)) |
| 210 | + |
| 211 | +class TestPeriodIndexOps(Ops): |
| 212 | + _allowed = '_allow_period_index_ops' |
| 213 | + |
| 214 | + def setUp(self): |
| 215 | + super(TestPeriodIndexOps, self).setUp() |
| 216 | + mask = lambda x: x._allow_datetime_index_ops or x._allow_period_index_ops |
| 217 | + self.is_valid_objs = [ o for o in self.objs if mask(o) ] |
| 218 | + self.not_valid_objs = [ o for o in self.objs if not mask(o) ] |
| 219 | + |
| 220 | + def test_ops_properties(self): |
| 221 | + self.check_ops_properties(['year','month','day','hour','minute','second','weekofyear','week','dayofweek','dayofyear','quarter']) |
| 222 | + self.check_ops_properties(['qyear'], lambda x: isinstance(x,PeriodIndex)) |
123 | 223 |
|
124 | 224 | if __name__ == '__main__':
|
125 | 225 | import nose
|
|
0 commit comments