|
| 1 | +import numpy as np |
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from pandas.util import testing as tm |
| 6 | +from pandas import (PeriodIndex, period_range, notnull, DatetimeIndex, NaT, |
| 7 | + Index, Period, Int64Index) |
| 8 | + |
| 9 | +from ..datetimelike import DatetimeLike |
| 10 | + |
| 11 | + |
| 12 | +class TestPeriodIndex(DatetimeLike, tm.TestCase): |
| 13 | + _holder = PeriodIndex |
| 14 | + _multiprocess_can_split_ = True |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + self.indices = dict(index=tm.makePeriodIndex(10)) |
| 18 | + self.setup_indices() |
| 19 | + |
| 20 | + def create_index(self): |
| 21 | + return period_range('20130101', periods=5, freq='D') |
| 22 | + |
| 23 | + def test_construction_base_constructor(self): |
| 24 | + # GH 13664 |
| 25 | + arr = [pd.Period('2011-01', freq='M'), pd.NaT, |
| 26 | + pd.Period('2011-03', freq='M')] |
| 27 | + tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr)) |
| 28 | + tm.assert_index_equal(pd.Index(np.array(arr)), |
| 29 | + pd.PeriodIndex(np.array(arr))) |
| 30 | + |
| 31 | + arr = [np.nan, pd.NaT, pd.Period('2011-03', freq='M')] |
| 32 | + tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr)) |
| 33 | + tm.assert_index_equal(pd.Index(np.array(arr)), |
| 34 | + pd.PeriodIndex(np.array(arr))) |
| 35 | + |
| 36 | + arr = [pd.Period('2011-01', freq='M'), pd.NaT, |
| 37 | + pd.Period('2011-03', freq='D')] |
| 38 | + tm.assert_index_equal(pd.Index(arr), pd.Index(arr, dtype=object)) |
| 39 | + |
| 40 | + tm.assert_index_equal(pd.Index(np.array(arr)), |
| 41 | + pd.Index(np.array(arr), dtype=object)) |
| 42 | + |
| 43 | + def test_astype(self): |
| 44 | + # GH 13149, GH 13209 |
| 45 | + idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') |
| 46 | + |
| 47 | + result = idx.astype(object) |
| 48 | + expected = Index([Period('2016-05-16', freq='D')] + |
| 49 | + [Period(NaT, freq='D')] * 3, dtype='object') |
| 50 | + tm.assert_index_equal(result, expected) |
| 51 | + |
| 52 | + result = idx.astype(int) |
| 53 | + expected = Int64Index([16937] + [-9223372036854775808] * 3, |
| 54 | + dtype=np.int64) |
| 55 | + tm.assert_index_equal(result, expected) |
| 56 | + |
| 57 | + idx = period_range('1990', '2009', freq='A') |
| 58 | + result = idx.astype('i8') |
| 59 | + self.assert_index_equal(result, Index(idx.asi8)) |
| 60 | + self.assert_numpy_array_equal(result.values, idx.asi8) |
| 61 | + |
| 62 | + def test_astype_raises(self): |
| 63 | + # GH 13149, GH 13209 |
| 64 | + idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') |
| 65 | + |
| 66 | + self.assertRaises(ValueError, idx.astype, str) |
| 67 | + self.assertRaises(ValueError, idx.astype, float) |
| 68 | + self.assertRaises(ValueError, idx.astype, 'timedelta64') |
| 69 | + self.assertRaises(ValueError, idx.astype, 'timedelta64[ns]') |
| 70 | + |
| 71 | + def test_shift(self): |
| 72 | + |
| 73 | + # test shift for PeriodIndex |
| 74 | + # GH8083 |
| 75 | + drange = self.create_index() |
| 76 | + result = drange.shift(1) |
| 77 | + expected = PeriodIndex(['2013-01-02', '2013-01-03', '2013-01-04', |
| 78 | + '2013-01-05', '2013-01-06'], freq='D') |
| 79 | + self.assert_index_equal(result, expected) |
| 80 | + |
| 81 | + def test_pickle_compat_construction(self): |
| 82 | + pass |
| 83 | + |
| 84 | + def test_get_loc(self): |
| 85 | + idx = pd.period_range('2000-01-01', periods=3) |
| 86 | + |
| 87 | + for method in [None, 'pad', 'backfill', 'nearest']: |
| 88 | + self.assertEqual(idx.get_loc(idx[1], method), 1) |
| 89 | + self.assertEqual( |
| 90 | + idx.get_loc(idx[1].asfreq('H', how='start'), method), 1) |
| 91 | + self.assertEqual(idx.get_loc(idx[1].to_timestamp(), method), 1) |
| 92 | + self.assertEqual( |
| 93 | + idx.get_loc(idx[1].to_timestamp().to_pydatetime(), method), 1) |
| 94 | + self.assertEqual(idx.get_loc(str(idx[1]), method), 1) |
| 95 | + |
| 96 | + idx = pd.period_range('2000-01-01', periods=5)[::2] |
| 97 | + self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest', |
| 98 | + tolerance='1 day'), 1) |
| 99 | + self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest', |
| 100 | + tolerance=pd.Timedelta('1D')), 1) |
| 101 | + self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest', |
| 102 | + tolerance=np.timedelta64(1, 'D')), 1) |
| 103 | + self.assertEqual(idx.get_loc('2000-01-02T12', method='nearest', |
| 104 | + tolerance=timedelta(1)), 1) |
| 105 | + with tm.assertRaisesRegexp(ValueError, 'must be convertible'): |
| 106 | + idx.get_loc('2000-01-10', method='nearest', tolerance='foo') |
| 107 | + |
| 108 | + msg = 'Input has different freq from PeriodIndex\\(freq=D\\)' |
| 109 | + with tm.assertRaisesRegexp(ValueError, msg): |
| 110 | + idx.get_loc('2000-01-10', method='nearest', tolerance='1 hour') |
| 111 | + with tm.assertRaises(KeyError): |
| 112 | + idx.get_loc('2000-01-10', method='nearest', tolerance='1 day') |
| 113 | + |
| 114 | + def test_where(self): |
| 115 | + i = self.create_index() |
| 116 | + result = i.where(notnull(i)) |
| 117 | + expected = i |
| 118 | + tm.assert_index_equal(result, expected) |
| 119 | + |
| 120 | + i2 = i.copy() |
| 121 | + i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(), |
| 122 | + freq='D') |
| 123 | + result = i.where(notnull(i2)) |
| 124 | + expected = i2 |
| 125 | + tm.assert_index_equal(result, expected) |
| 126 | + |
| 127 | + def test_where_other(self): |
| 128 | + |
| 129 | + i = self.create_index() |
| 130 | + for arr in [np.nan, pd.NaT]: |
| 131 | + result = i.where(notnull(i), other=np.nan) |
| 132 | + expected = i |
| 133 | + tm.assert_index_equal(result, expected) |
| 134 | + |
| 135 | + i2 = i.copy() |
| 136 | + i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(), |
| 137 | + freq='D') |
| 138 | + result = i.where(notnull(i2), i2) |
| 139 | + tm.assert_index_equal(result, i2) |
| 140 | + |
| 141 | + i2 = i.copy() |
| 142 | + i2 = pd.PeriodIndex([pd.NaT, pd.NaT] + i[2:].tolist(), |
| 143 | + freq='D') |
| 144 | + result = i.where(notnull(i2), i2.values) |
| 145 | + tm.assert_index_equal(result, i2) |
| 146 | + |
| 147 | + def test_get_indexer(self): |
| 148 | + idx = pd.period_range('2000-01-01', periods=3).asfreq('H', how='start') |
| 149 | + tm.assert_numpy_array_equal(idx.get_indexer(idx), |
| 150 | + np.array([0, 1, 2], dtype=np.intp)) |
| 151 | + |
| 152 | + target = pd.PeriodIndex(['1999-12-31T23', '2000-01-01T12', |
| 153 | + '2000-01-02T01'], freq='H') |
| 154 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'pad'), |
| 155 | + np.array([-1, 0, 1], dtype=np.intp)) |
| 156 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'backfill'), |
| 157 | + np.array([0, 1, 2], dtype=np.intp)) |
| 158 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest'), |
| 159 | + np.array([0, 1, 1], dtype=np.intp)) |
| 160 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest', |
| 161 | + tolerance='1 hour'), |
| 162 | + np.array([0, -1, 1], dtype=np.intp)) |
| 163 | + |
| 164 | + msg = 'Input has different freq from PeriodIndex\\(freq=H\\)' |
| 165 | + with self.assertRaisesRegexp(ValueError, msg): |
| 166 | + idx.get_indexer(target, 'nearest', tolerance='1 minute') |
| 167 | + |
| 168 | + tm.assert_numpy_array_equal(idx.get_indexer(target, 'nearest', |
| 169 | + tolerance='1 day'), |
| 170 | + np.array([0, 1, 1], dtype=np.intp)) |
| 171 | + |
| 172 | + def test_repeat(self): |
| 173 | + # GH10183 |
| 174 | + idx = pd.period_range('2000-01-01', periods=3, freq='D') |
| 175 | + res = idx.repeat(3) |
| 176 | + exp = PeriodIndex(idx.values.repeat(3), freq='D') |
| 177 | + self.assert_index_equal(res, exp) |
| 178 | + self.assertEqual(res.freqstr, 'D') |
| 179 | + |
| 180 | + def test_period_index_indexer(self): |
| 181 | + # GH4125 |
| 182 | + idx = pd.period_range('2002-01', '2003-12', freq='M') |
| 183 | + df = pd.DataFrame(pd.np.random.randn(24, 10), index=idx) |
| 184 | + self.assert_frame_equal(df, df.loc[idx]) |
| 185 | + self.assert_frame_equal(df, df.loc[list(idx)]) |
| 186 | + self.assert_frame_equal(df, df.loc[list(idx)]) |
| 187 | + self.assert_frame_equal(df.iloc[0:5], df.loc[idx[0:5]]) |
| 188 | + self.assert_frame_equal(df, df.loc[list(idx)]) |
| 189 | + |
| 190 | + def test_fillna_period(self): |
| 191 | + # GH 11343 |
| 192 | + idx = pd.PeriodIndex(['2011-01-01 09:00', pd.NaT, |
| 193 | + '2011-01-01 11:00'], freq='H') |
| 194 | + |
| 195 | + exp = pd.PeriodIndex(['2011-01-01 09:00', '2011-01-01 10:00', |
| 196 | + '2011-01-01 11:00'], freq='H') |
| 197 | + self.assert_index_equal( |
| 198 | + idx.fillna(pd.Period('2011-01-01 10:00', freq='H')), exp) |
| 199 | + |
| 200 | + exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'), 'x', |
| 201 | + pd.Period('2011-01-01 11:00', freq='H')], dtype=object) |
| 202 | + self.assert_index_equal(idx.fillna('x'), exp) |
| 203 | + |
| 204 | + exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'), |
| 205 | + pd.Period('2011-01-01', freq='D'), |
| 206 | + pd.Period('2011-01-01 11:00', freq='H')], dtype=object) |
| 207 | + self.assert_index_equal(idx.fillna(pd.Period('2011-01-01', freq='D')), |
| 208 | + exp) |
| 209 | + |
| 210 | + def test_no_millisecond_field(self): |
| 211 | + with self.assertRaises(AttributeError): |
| 212 | + DatetimeIndex.millisecond |
| 213 | + |
| 214 | + with self.assertRaises(AttributeError): |
| 215 | + DatetimeIndex([]).millisecond |
| 216 | + |
| 217 | + def test_difference_freq(self): |
| 218 | + # GH14323: difference of Period MUST preserve frequency |
| 219 | + # but the ability to union results must be preserved |
| 220 | + |
| 221 | + index = period_range("20160920", "20160925", freq="D") |
| 222 | + |
| 223 | + other = period_range("20160921", "20160924", freq="D") |
| 224 | + expected = PeriodIndex(["20160920", "20160925"], freq='D') |
| 225 | + idx_diff = index.difference(other) |
| 226 | + tm.assert_index_equal(idx_diff, expected) |
| 227 | + tm.assert_attr_equal('freq', idx_diff, expected) |
| 228 | + |
| 229 | + other = period_range("20160922", "20160925", freq="D") |
| 230 | + idx_diff = index.difference(other) |
| 231 | + expected = PeriodIndex(["20160920", "20160921"], freq='D') |
| 232 | + tm.assert_index_equal(idx_diff, expected) |
| 233 | + tm.assert_attr_equal('freq', idx_diff, expected) |
0 commit comments