ENH/BUG: Period and PeriodIndex ops supports timedelta-like by sinhrks · Pull Request #7966 · pandas-dev/pandas (original) (raw)
Allow Period
and PeriodIndex
add
and sub
to support timedelta
-like. If period freq is offsets.Tick
, offsets can be added if the result can have same freq. Otherwise, ValueError
will be raised.
idx = pd.period_range('2014-07-01 09:00', periods=5, freq='H')
idx + pd.offsets.Hour(2)
# <class 'pandas.tseries.period.PeriodIndex'>
# [2014-07-01 11:00, ..., 2014-07-01 15:00]
# Length: 5, Freq: H
idx + datetime.timedelta(minutes=120)
# <class 'pandas.tseries.period.PeriodIndex'>
# [2014-07-01 11:00, ..., 2014-07-01 15:00]
# Length: 5, Freq: H
idx + np.timedelta64(7200, 's')
# <class 'pandas.tseries.period.PeriodIndex'>
# [2014-07-01 11:00, ..., 2014-07-01 15:00]
# Length: 5, Freq: H
idx + pd.offsets.Minute(5)
# ValueError: Input has different freq from PeriodIndex(freq=H)
If period freq isn't Tick
, only the same offset can be added. Otherwise, ValueError
will be raised.
idx = pd.period_range('2014-07', periods=5, freq='M')
idx + pd.offsets.MonthEnd(3)
# <class 'pandas.tseries.period.PeriodIndex'>
# [2014-10, ..., 2015-02]
# Length: 5, Freq: M
idx + pd.offsets.MonthBegin(3)
# ValueError: Input has different freq from PeriodIndex(freq=M)