ENH/BUG: Period and PeriodIndex ops supports timedelta-like #7966

Merged
merged 1 commit into from Aug 10, 2014

Conversation

Projects
None yet
2 participants
Member

sinhrks commented Aug 8, 2014

Must be revisited after #7954 to remove _skip_if_not_numpy17_friendly

Closes #7740.

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)

jreback added this to the 0.15.0 milestone Aug 8, 2014

@jreback jreback commented on the diff Aug 8, 2014

pandas/tseries/period.py
@@ -862,6 +896,22 @@ def to_timestamp(self, freq=None, how='start'):
new_data = tslib.periodarr_to_dt64arr(new_data.values, base)
return DatetimeIndex(new_data, freq='infer', name=self.name)
+ def _add_delta(self, other):
+ if isinstance(other, (timedelta, np.timedelta64, offsets.Tick)):
@jreback

jreback Aug 8, 2014

Contributor

hmm, does it make sense to push _add_delta (and DatetimeIndex._add_delta) to base into order to combine them (its currently marked as non-implemented in DatetimeIndexOpsMixin)?

@sinhrks

sinhrks Aug 8, 2014

Member

I feel this gets more complicated, because each _add_delta handles its internal representation (UTC time in DatetimeIndex and ordinal in PeriodIndex).
https://github.com/pydata/pandas/blob/master/pandas/tseries/index.py#L615

@jreback

jreback Aug 8, 2014

Contributor

ok, not a big deal (always like to have centralized stuff like this), but sometimes makes too complicated. if in the future you think can integrate then ok.

@jreback jreback and 1 other commented on an outdated diff Aug 8, 2014

doc/source/v0.15.0.txt
@@ -271,7 +271,7 @@ Enhancements
-
+- ``Period`` and ``PeriodIndex`` supports addition/subtraction with ``timedelta``-likes (:issue:`7966`)
@jreback

jreback Aug 8, 2014

Contributor

maybe give the example here, (and add to timeseries.rst as well)

@sinhrks

sinhrks Aug 9, 2014

Member

Yeah, added docs.

@jreback jreback added a commit that referenced this pull request Aug 10, 2014

@jreback jreback Merge pull request #7966 from sinhrks/period_delta
ENH/BUG: Period and PeriodIndex ops supports timedelta-like
e800fe1

@jreback jreback merged commit e800fe1 into pandas-dev:master Aug 10, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
Contributor

jreback commented Aug 10, 2014

thanks @sinhrks

sinhrks deleted the sinhrks:period_delta branch Aug 10, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment