50
pandas: powerful Python data analysis toolkit, Release 0.18.1
Out[264]: Period('2011-11', '2M')
In [265]: p == pd.Period('2012-01', freq='3M')
---------------------------------------------------------------------------
IncompatibleFrequency
Traceback (most recent call last)
<ipython-input-265-ff54ce3238f5> in <module>()
----> 1 p == pd.Period('2012-01', freq='3M')
/Users/tom.augspurger/miniconda3/envs/docs/lib/python2.7/site-packages/pandas/pandas/src/period.pyx in pandas._period.Period.__richcmp__ (pandas/src/period.c:12486)()
769
if other.freq != self.freq:
770
msg = _DIFFERENT_FREQ.format(self.freqstr, other.freqstr)
--> 771
raise IncompatibleFrequency(msg)
772
if self.ordinal == tslib.iNaT or other.ordinal == tslib.iNaT:
773
return _nat_scalar_rules[op]
IncompatibleFrequency: Input has different freq=3M from Period(freq=2M)
If Period freqis daily orhigher(D,H, T,S,L, U,N),offsets and timedelta-like can be addedif the result can
have the same freq. Otherwise,ValueError will be raised.
In [266]: p = pd.Period('2014-07-01 09:00', freq='H')
In [267]: p + Hour(2)
Out[267]: Period('2014-07-01 11:00', 'H')
In [268]: p + timedelta(minutes=120)
Out[268]: Period('2014-07-01 11:00', 'H')
In [269]: p + np.timedelta64(7200, 's')
Out[269]: Period('2014-07-01 11:00', 'H')
In [1]: : p + Minute(5)
Traceback
...
ValueError: Input has different freq from Period(freq=H)
If Period has other freqs,only the same offsets can be added. Otherwise,ValueError will be raised.
In [270]: p = pd.Period('2014-07', freq='M')
In [271]: p + MonthEnd(3)
Out[271]: Period('2014-10', 'M')
In [1]: : p + MonthBegin(3)
Traceback
...
ValueError: Input has different freq from Period(freq=M)
Takingthedifference ofPeriod instances with the same frequencywill returnthenumber offrequencyunits between
them:
In [272]: pd.Period('2012', freq='A-DEC') - pd.Period('2002', freq='A-DEC')
Out[272]: 10
20.10.2 PeriodIndex and period_range
Regular sequences of Period objects can be collected in a PeriodIndex, which can be constructed using the
period_range convenience function:
20.10. Time Span Representation
697