|
10 | 10 | from pandas.core.categorical import Categorical |
11 | 11 | from pandas.core.index import Index, Int64Index, MultiIndex |
12 | 12 | from pandas.core.frame import DataFrame |
| 13 | +from pandas.tseries.period import PeriodIndex |
13 | 14 | from pandas.util.testing import assert_almost_equal |
14 | 15 | import pandas.core.common as com |
15 | 16 |
|
@@ -180,6 +181,37 @@ def test_empty_print(self): |
180 | 181 | "Index([], dtype=object)") |
181 | 182 | self.assertEqual(repr(factor), expected) |
182 | 183 |
|
| 184 | + def test_periodindex(self): |
| 185 | + idx1 = PeriodIndex(['2014-01', '2014-01', '2014-02', '2014-02', |
| 186 | + '2014-03', '2014-03'], freq='M') |
| 187 | + cat1 = Categorical.from_array(idx1) |
| 188 | + |
| 189 | + exp_arr = np.array([0, 0, 1, 1, 2, 2]) |
| 190 | + exp_idx = PeriodIndex(['2014-01', '2014-02', '2014-03'], freq='M') |
| 191 | + |
| 192 | + self.assert_numpy_array_equal(cat1.labels, exp_arr) |
| 193 | + self.assert_(cat1.levels.equals(exp_idx)) |
| 194 | + |
| 195 | + idx2 = PeriodIndex(['2014-03', '2014-03', '2014-02', '2014-01', |
| 196 | + '2014-03', '2014-01'], freq='M') |
| 197 | + cat2 = Categorical.from_array(idx2) |
| 198 | + |
| 199 | + exp_arr = np.array([2, 2, 1, 0, 2, 0]) |
| 200 | + |
| 201 | + self.assert_numpy_array_equal(cat2.labels, exp_arr) |
| 202 | + self.assert_(cat2.levels.equals(exp_idx)) |
| 203 | + |
| 204 | + idx3 = PeriodIndex(['2013-12', '2013-11', '2013-10', '2013-09', |
| 205 | + '2013-08', '2013-07', '2013-05'], freq='M') |
| 206 | + cat3 = Categorical.from_array(idx3) |
| 207 | + |
| 208 | + exp_arr = np.array([6, 5, 4, 3, 2, 1, 0]) |
| 209 | + exp_idx = PeriodIndex(['2013-05', '2013-07', '2013-08', '2013-09', |
| 210 | + '2013-10', '2013-11', '2013-12'], freq='M') |
| 211 | + |
| 212 | + self.assert_numpy_array_equal(cat3.labels, exp_arr) |
| 213 | + self.assert_(cat3.levels.equals(exp_idx)) |
| 214 | + |
183 | 215 |
|
184 | 216 | if __name__ == '__main__': |
185 | 217 | import nose |
|
0 commit comments