Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 10, 2014

Conversation

sinhrks
Copy link
Member

@sinhrks 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 jreback added this to the 0.15.0 milestone Aug 8, 2014
@@ -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)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 added a commit that referenced this pull request Aug 10, 2014
ENH/BUG: Period and PeriodIndex ops supports timedelta-like
@jreback jreback merged commit e800fe1 into pandas-dev:master Aug 10, 2014
@jreback
Copy link
Contributor

jreback commented Aug 10, 2014

thanks @sinhrks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Period Period data type Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Period ops with np.timedelta64 cause unexpected result
2 participants