Python pandas.PeriodIndex() Examples
The following are 30
code examples of pandas.PeriodIndex().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pandas
, or try the search function
.
Example #1
Source File: test_construction.py From recruit with Apache License 2.0 | 6 votes |
def test_constructor_incompat_freq(self): msg = "Input has different freq=D from PeriodIndex\\(freq=M\\)" with pytest.raises(period.IncompatibleFrequency, match=msg): PeriodIndex([Period('2011-01', freq='M'), pd.NaT, Period('2011-01', freq='D')]) with pytest.raises(period.IncompatibleFrequency, match=msg): PeriodIndex(np.array([Period('2011-01', freq='M'), pd.NaT, Period('2011-01', freq='D')])) # first element is pd.NaT with pytest.raises(period.IncompatibleFrequency, match=msg): PeriodIndex([pd.NaT, Period('2011-01', freq='M'), Period('2011-01', freq='D')]) with pytest.raises(period.IncompatibleFrequency, match=msg): PeriodIndex(np.array([pd.NaT, Period('2011-01', freq='M'), Period('2011-01', freq='D')]))
Example #2
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_get_indexer_non_unique(self): # GH 17717 p1 = pd.Period('2017-09-02') p2 = pd.Period('2017-09-03') p3 = pd.Period('2017-09-04') p4 = pd.Period('2017-09-05') idx1 = pd.PeriodIndex([p1, p2, p1]) idx2 = pd.PeriodIndex([p2, p1, p3, p4]) result = idx1.get_indexer_non_unique(idx2) expected_indexer = np.array([1, 0, 2, -1, -1], dtype=np.intp) expected_missing = np.array([2, 3], dtype=np.int64) tm.assert_numpy_array_equal(result[0], expected_indexer) tm.assert_numpy_array_equal(result[1], expected_missing) # TODO: This method came from test_period; de-dup with version above
Example #3
Source File: test_arithmetic.py From recruit with Apache License 2.0 | 6 votes |
def test_shift_corner_cases(self): # GH#9903 idx = pd.PeriodIndex([], name='xxx', freq='H') with pytest.raises(TypeError): # period shift doesn't accept freq idx.shift(1, freq='H') tm.assert_index_equal(idx.shift(0), idx) tm.assert_index_equal(idx.shift(3), idx) idx = pd.PeriodIndex(['2011-01-01 10:00', '2011-01-01 11:00' '2011-01-01 12:00'], name='xxx', freq='H') tm.assert_index_equal(idx.shift(0), idx) exp = pd.PeriodIndex(['2011-01-01 13:00', '2011-01-01 14:00' '2011-01-01 15:00'], name='xxx', freq='H') tm.assert_index_equal(idx.shift(3), exp) exp = pd.PeriodIndex(['2011-01-01 07:00', '2011-01-01 08:00' '2011-01-01 09:00'], name='xxx', freq='H') tm.assert_index_equal(idx.shift(-3), exp)
Example #4
Source File: test_astype.py From recruit with Apache License 2.0 | 6 votes |
def test_astype_object(self): idx = pd.PeriodIndex([], freq='M') exp = np.array([], dtype=object) tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M') exp = np.array([pd.Period('2011-01', freq='M'), pd.NaT], dtype=object) tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) exp = np.array([pd.Period('2011-01-01', freq='D'), pd.NaT], dtype=object) idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D') tm.assert_numpy_array_equal(idx.astype(object).values, exp) tm.assert_numpy_array_equal(idx._mpl_repr(), exp) # TODO: de-duplicate this version (from test_ops) with the one above # (from test_period)
Example #5
Source File: test_indexing.py From recruit with Apache License 2.0 | 6 votes |
def test_is_monotonic_decreasing(self): # GH 17717 p0 = pd.Period('2017-09-01') p1 = pd.Period('2017-09-02') p2 = pd.Period('2017-09-03') idx_inc0 = pd.PeriodIndex([p0, p1, p2]) idx_inc1 = pd.PeriodIndex([p0, p1, p1]) idx_dec0 = pd.PeriodIndex([p2, p1, p0]) idx_dec1 = pd.PeriodIndex([p2, p1, p1]) idx = pd.PeriodIndex([p1, p2, p0]) assert idx_inc0.is_monotonic_decreasing is False assert idx_inc1.is_monotonic_decreasing is False assert idx_dec0.is_monotonic_decreasing is True assert idx_dec1.is_monotonic_decreasing is True assert idx.is_monotonic_decreasing is False
Example #6
Source File: test_astype.py From recruit with Apache License 2.0 | 6 votes |
def test_astype_conversion(self): # GH#13149, GH#13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') result = idx.astype(object) expected = Index([Period('2016-05-16', freq='D')] + [Period(NaT, freq='D')] * 3, dtype='object') tm.assert_index_equal(result, expected) result = idx.astype(np.int64) expected = Int64Index([16937] + [-9223372036854775808] * 3, dtype=np.int64) tm.assert_index_equal(result, expected) result = idx.astype(str) expected = Index(str(x) for x in idx) tm.assert_index_equal(result, expected) idx = period_range('1990', '2009', freq='A') result = idx.astype('i8') tm.assert_index_equal(result, Index(idx.asi8)) tm.assert_numpy_array_equal(result.values, idx.asi8)
Example #7
Source File: test_construction.py From recruit with Apache License 2.0 | 6 votes |
def test_construction_base_constructor(self): # GH 13664 arr = [pd.Period('2011-01', freq='M'), pd.NaT, pd.Period('2011-03', freq='M')] tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr)) tm.assert_index_equal(pd.Index(np.array(arr)), pd.PeriodIndex(np.array(arr))) arr = [np.nan, pd.NaT, pd.Period('2011-03', freq='M')] tm.assert_index_equal(pd.Index(arr), pd.PeriodIndex(arr)) tm.assert_index_equal(pd.Index(np.array(arr)), pd.PeriodIndex(np.array(arr))) arr = [pd.Period('2011-01', freq='M'), pd.NaT, pd.Period('2011-03', freq='D')] tm.assert_index_equal(pd.Index(arr), pd.Index(arr, dtype=object)) tm.assert_index_equal(pd.Index(np.array(arr)), pd.Index(np.array(arr), dtype=object))
Example #8
Source File: test_period.py From recruit with Apache License 2.0 | 6 votes |
def test_fillna_period(self): # GH 11343 idx = pd.PeriodIndex(['2011-01-01 09:00', pd.NaT, '2011-01-01 11:00'], freq='H') exp = pd.PeriodIndex(['2011-01-01 09:00', '2011-01-01 10:00', '2011-01-01 11:00'], freq='H') tm.assert_index_equal( idx.fillna(pd.Period('2011-01-01 10:00', freq='H')), exp) exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'), 'x', pd.Period('2011-01-01 11:00', freq='H')], dtype=object) tm.assert_index_equal(idx.fillna('x'), exp) exp = pd.Index([pd.Period('2011-01-01 09:00', freq='H'), pd.Period('2011-01-01', freq='D'), pd.Period('2011-01-01 11:00', freq='H')], dtype=object) tm.assert_index_equal(idx.fillna( pd.Period('2011-01-01', freq='D')), exp)
Example #9
Source File: test_period.py From recruit with Apache License 2.0 | 6 votes |
def test_difference_freq(self, sort): # GH14323: difference of Period MUST preserve frequency # but the ability to union results must be preserved index = period_range("20160920", "20160925", freq="D") other = period_range("20160921", "20160924", freq="D") expected = PeriodIndex(["20160920", "20160925"], freq='D') idx_diff = index.difference(other, sort) tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal('freq', idx_diff, expected) other = period_range("20160922", "20160925", freq="D") idx_diff = index.difference(other, sort) expected = PeriodIndex(["20160920", "20160921"], freq='D') tm.assert_index_equal(idx_diff, expected) tm.assert_attr_equal('freq', idx_diff, expected)
Example #10
Source File: test_tools.py From recruit with Apache License 2.0 | 6 votes |
def test_period_astype_to_timestamp(self): pi = pd.PeriodIndex(['2011-01', '2011-02', '2011-03'], freq='M') exp = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01']) tm.assert_index_equal(pi.astype('datetime64[ns]'), exp) exp = pd.DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31']) exp = exp + Timedelta(1, 'D') - Timedelta(1, 'ns') tm.assert_index_equal(pi.astype('datetime64[ns]', how='end'), exp) exp = pd.DatetimeIndex(['2011-01-01', '2011-02-01', '2011-03-01'], tz='US/Eastern') res = pi.astype('datetime64[ns, US/Eastern]') tm.assert_index_equal(pi.astype('datetime64[ns, US/Eastern]'), exp) exp = pd.DatetimeIndex(['2011-01-31', '2011-02-28', '2011-03-31'], tz='US/Eastern') exp = exp + Timedelta(1, 'D') - Timedelta(1, 'ns') res = pi.astype('datetime64[ns, US/Eastern]', how='end') tm.assert_index_equal(res, exp)
Example #11
Source File: test_period.py From recruit with Apache License 2.0 | 6 votes |
def test_view_asi8(self): idx = pd.PeriodIndex([], freq='M') exp = np.array([], dtype=np.int64) tm.assert_numpy_array_equal(idx.view('i8'), exp) tm.assert_numpy_array_equal(idx.asi8, exp) idx = pd.PeriodIndex(['2011-01', pd.NaT], freq='M') exp = np.array([492, -9223372036854775808], dtype=np.int64) tm.assert_numpy_array_equal(idx.view('i8'), exp) tm.assert_numpy_array_equal(idx.asi8, exp) exp = np.array([14975, -9223372036854775808], dtype=np.int64) idx = pd.PeriodIndex(['2011-01-01', pd.NaT], freq='D') tm.assert_numpy_array_equal(idx.view('i8'), exp) tm.assert_numpy_array_equal(idx.asi8, exp)
Example #12
Source File: test_tools.py From recruit with Apache License 2.0 | 6 votes |
def test_to_timestamp_pi_nat(self): # GH#7228 index = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M', name='idx') result = index.to_timestamp('D') expected = DatetimeIndex([pd.NaT, datetime(2011, 1, 1), datetime(2011, 2, 1)], name='idx') tm.assert_index_equal(result, expected) assert result.name == 'idx' result2 = result.to_period(freq='M') tm.assert_index_equal(result2, index) assert result2.name == 'idx' result3 = result.to_period(freq='3M') exp = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='3M', name='idx') tm.assert_index_equal(result3, exp) assert result3.freqstr == '3M' msg = ('Frequency must be positive, because it' ' represents span: -2A') with pytest.raises(ValueError, match=msg): result.to_period(freq='-2A')
Example #13
Source File: test_period.py From recruit with Apache License 2.0 | 6 votes |
def test_index_duplicate_periods(self): # monotonic idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN') ts = Series(np.random.randn(len(idx)), index=idx) result = ts[2007] expected = ts[1:3] tm.assert_series_equal(result, expected) result[:] = 1 assert (ts[1:3] == 1).all() # not monotonic idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq='A-JUN') ts = Series(np.random.randn(len(idx)), index=idx) result = ts[2007] expected = ts[idx == 2007] tm.assert_series_equal(result, expected)
Example #14
Source File: test_setops.py From recruit with Apache License 2.0 | 6 votes |
def test_union_misc(self, sort): index = period_range('1/1/2000', '1/20/2000', freq='D') result = index[:-5].union(index[10:], sort=sort) tm.assert_index_equal(result, index) # not in order result = _permute(index[:-5]).union(_permute(index[10:]), sort=sort) if sort is None: tm.assert_index_equal(result, index) assert tm.equalContents(result, index) # raise if different frequencies index = period_range('1/1/2000', '1/20/2000', freq='D') index2 = period_range('1/1/2000', '1/20/2000', freq='W-WED') with pytest.raises(period.IncompatibleFrequency): index.union(index2, sort=sort) msg = 'can only call with other PeriodIndex-ed objects' with pytest.raises(ValueError, match=msg): index.join(index.to_timestamp()) index3 = period_range('1/1/2000', '1/20/2000', freq='2D') with pytest.raises(period.IncompatibleFrequency): index.join(index3)
Example #15
Source File: test_tools.py From recruit with Apache License 2.0 | 6 votes |
def test_searchsorted(self, freq): pidx = pd.PeriodIndex(['2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04', '2014-01-05'], freq=freq) p1 = pd.Period('2014-01-01', freq=freq) assert pidx.searchsorted(p1) == 0 p2 = pd.Period('2014-01-04', freq=freq) assert pidx.searchsorted(p2) == 3 msg = "Input has different freq=H from PeriodIndex" with pytest.raises(period.IncompatibleFrequency, match=msg): pidx.searchsorted(pd.Period('2014-01-01', freq='H')) msg = "Input has different freq=5D from PeriodIndex" with pytest.raises(period.IncompatibleFrequency, match=msg): pidx.searchsorted(pd.Period('2014-01-01', freq='5D'))
Example #16
Source File: test_astype.py From recruit with Apache License 2.0 | 6 votes |
def test_to_period_nofreq(self): idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-04']) with pytest.raises(ValueError): idx.to_period() idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03'], freq='infer') assert idx.freqstr == 'D' expected = pd.PeriodIndex(['2000-01-01', '2000-01-02', '2000-01-03'], freq='D') tm.assert_index_equal(idx.to_period(), expected) # GH 7606 idx = DatetimeIndex(['2000-01-01', '2000-01-02', '2000-01-03']) assert idx.freqstr is None tm.assert_index_equal(idx.to_period(), expected)
Example #17
Source File: test_period.py From recruit with Apache License 2.0 | 6 votes |
def test_pindex_multiples(self): with tm.assert_produces_warning(FutureWarning): pi = PeriodIndex(start='1/1/11', end='12/31/11', freq='2M') expected = PeriodIndex(['2011-01', '2011-03', '2011-05', '2011-07', '2011-09', '2011-11'], freq='2M') tm.assert_index_equal(pi, expected) assert pi.freq == offsets.MonthEnd(2) assert pi.freqstr == '2M' pi = period_range(start='1/1/11', end='12/31/11', freq='2M') tm.assert_index_equal(pi, expected) assert pi.freq == offsets.MonthEnd(2) assert pi.freqstr == '2M' pi = period_range(start='1/1/11', periods=6, freq='2M') tm.assert_index_equal(pi, expected) assert pi.freq == offsets.MonthEnd(2) assert pi.freqstr == '2M'
Example #18
Source File: test_period.py From recruit with Apache License 2.0 | 6 votes |
def test_is_full(self): index = PeriodIndex([2005, 2007, 2009], freq='A') assert not index.is_full index = PeriodIndex([2005, 2006, 2007], freq='A') assert index.is_full index = PeriodIndex([2005, 2005, 2007], freq='A') assert not index.is_full index = PeriodIndex([2005, 2005, 2006], freq='A') assert index.is_full index = PeriodIndex([2006, 2005, 2005], freq='A') pytest.raises(ValueError, getattr, index, 'is_full') assert index[:0].is_full
Example #19
Source File: test_construction.py From recruit with Apache License 2.0 | 6 votes |
def test_map_with_string_constructor(self): raw = [2005, 2007, 2009] index = PeriodIndex(raw, freq='A') types = str, if PY3: # unicode types += text_type, for t in types: expected = Index(lmap(t, raw)) res = index.map(t) # should return an Index assert isinstance(res, Index) # preserve element types assert all(isinstance(resi, t) for resi in res) # lastly, values should compare equal tm.assert_index_equal(res, expected)
Example #20
Source File: test_period.py From recruit with Apache License 2.0 | 5 votes |
def test_index_unique(self): idx = PeriodIndex([2000, 2007, 2007, 2009, 2009], freq='A-JUN') expected = PeriodIndex([2000, 2007, 2009], freq='A-JUN') tm.assert_index_equal(idx.unique(), expected) assert idx.nunique() == 3 idx = PeriodIndex([2000, 2007, 2007, 2009, 2007], freq='A-JUN', tz='US/Eastern') expected = PeriodIndex([2000, 2007, 2009], freq='A-JUN', tz='US/Eastern') tm.assert_index_equal(idx.unique(), expected) assert idx.nunique() == 3
Example #21
Source File: test_asfreq.py From recruit with Apache License 2.0 | 5 votes |
def test_asfreq_mult_pi(self, freq): pi = PeriodIndex(['2001-01', '2001-02', 'NaT', '2001-03'], freq='2M') result = pi.asfreq(freq) exp = PeriodIndex(['2001-02-28', '2001-03-31', 'NaT', '2001-04-30'], freq=freq) tm.assert_index_equal(result, exp) assert result.freq == exp.freq result = pi.asfreq(freq, how='S') exp = PeriodIndex(['2001-01-01', '2001-02-01', 'NaT', '2001-03-01'], freq=freq) tm.assert_index_equal(result, exp) assert result.freq == exp.freq
Example #22
Source File: test_asfreq.py From recruit with Apache License 2.0 | 5 votes |
def test_asfreq_combined_pi(self): pi = pd.PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'], freq='H') exp = PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'], freq='25H') for freq, how in zip(['1D1H', '1H1D'], ['S', 'E']): result = pi.asfreq(freq, how=how) tm.assert_index_equal(result, exp) assert result.freq == exp.freq for freq in ['1D1H', '1H1D']: pi = pd.PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'], freq=freq) result = pi.asfreq('H') exp = PeriodIndex(['2001-01-02 00:00', '2001-01-03 02:00', 'NaT'], freq='H') tm.assert_index_equal(result, exp) assert result.freq == exp.freq pi = pd.PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'], freq=freq) result = pi.asfreq('H', how='S') exp = PeriodIndex(['2001-01-01 00:00', '2001-01-02 02:00', 'NaT'], freq='H') tm.assert_index_equal(result, exp) assert result.freq == exp.freq
Example #23
Source File: test_period.py From recruit with Apache License 2.0 | 5 votes |
def test_contains_nat(self): # see gh-13582 idx = period_range('2007-01', freq='M', periods=10) assert pd.NaT not in idx assert None not in idx assert float('nan') not in idx assert np.nan not in idx idx = pd.PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='M') assert pd.NaT in idx assert None in idx assert float('nan') in idx assert np.nan in idx
Example #24
Source File: test_arithmetic.py From recruit with Apache License 2.0 | 5 votes |
def test_shift_nat(self): idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'], freq='M', name='idx') result = idx.shift(1) expected = PeriodIndex(['2011-02', '2011-03', 'NaT', '2011-05'], freq='M', name='idx') tm.assert_index_equal(result, expected) assert result.name == expected.name
Example #25
Source File: test_asfreq.py From recruit with Apache License 2.0 | 5 votes |
def test_asfreq_nat(self): idx = PeriodIndex(['2011-01', '2011-02', 'NaT', '2011-04'], freq='M') result = idx.asfreq(freq='Q') expected = PeriodIndex(['2011Q1', '2011Q1', 'NaT', '2011Q2'], freq='Q') tm.assert_index_equal(result, expected)
Example #26
Source File: test_astype.py From recruit with Apache License 2.0 | 5 votes |
def test_astype_raises(self, dtype): # GH#13149, GH#13209 idx = PeriodIndex(['2016-05-16', 'NaT', NaT, np.NaN], freq='D') msg = 'Cannot cast PeriodArray to dtype' with pytest.raises(TypeError, match=msg): idx.astype(dtype)
Example #27
Source File: test_tools.py From recruit with Apache License 2.0 | 5 votes |
def test_to_timestamp_pi_mult(self): idx = PeriodIndex(['2011-01', 'NaT', '2011-02'], freq='2M', name='idx') result = idx.to_timestamp() expected = DatetimeIndex(['2011-01-01', 'NaT', '2011-02-01'], name='idx') tm.assert_index_equal(result, expected) result = idx.to_timestamp(how='E') expected = DatetimeIndex(['2011-02-28', 'NaT', '2011-03-31'], name='idx') expected = expected + Timedelta(1, 'D') - Timedelta(1, 'ns') tm.assert_index_equal(result, expected)
Example #28
Source File: test_tools.py From recruit with Apache License 2.0 | 5 votes |
def test_tolist(self): index = period_range(freq='A', start='1/1/2001', end='12/1/2009') rs = index.tolist() for x in rs: assert isinstance(x, Period) recon = PeriodIndex(rs) tm.assert_index_equal(index, recon)
Example #29
Source File: test_tools.py From recruit with Apache License 2.0 | 5 votes |
def test_to_timestamp_to_period_astype(self): idx = DatetimeIndex([pd.NaT, '2011-01-01', '2011-02-01'], name='idx') res = idx.astype('period[M]') exp = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='M', name='idx') tm.assert_index_equal(res, exp) res = idx.astype('period[3M]') exp = PeriodIndex(['NaT', '2011-01', '2011-02'], freq='3M', name='idx') tm.assert_index_equal(res, exp)
Example #30
Source File: test_construction.py From recruit with Apache License 2.0 | 5 votes |
def test_constructor_error(self): start = Period('02-Apr-2005', 'B') end_intv = Period('2006-12-31', ('w', 1)) msg = 'start and end must have same freq' with pytest.raises(ValueError, match=msg): PeriodIndex(start=start, end=end_intv) msg = ('Of the three parameters: start, end, and periods, ' 'exactly two must be specified') with pytest.raises(ValueError, match=msg): PeriodIndex(start=start)