Skip to content

ENH/API: Decide what to return Period - Period subtraction #13077

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

Closed
sinhrks opened this issue May 4, 2016 · 6 comments · Fixed by #20049
Closed

ENH/API: Decide what to return Period - Period subtraction #13077

sinhrks opened this issue May 4, 2016 · 6 comments · Fixed by #20049
Labels
API Design Enhancement Period Period data type Timedelta Timedelta data type
Milestone

Comments

@sinhrks
Copy link
Member

sinhrks commented May 4, 2016

Code Sample, a copy-pastable example if possible

Split from #13071. Currently it returns int and loses freq info.

pd.Period('2011-03', freq='M') - pd.Period('2011-01', freq='M') 
#2

Expected Output

Current ideas are:

  1. Return Timedelta adding support of Monthly freq.
  2. Return Perioddelta newly created.
  3. Return DateOffset.

output of pd.show_versions()

on current master.

CC: @jreback @MaximilianR

@sinhrks sinhrks added Enhancement API Design Timedelta Timedelta data type Period Period data type labels May 4, 2016
@sinhrks sinhrks added this to the 0.19.0 milestone May 4, 2016
@shoyer
Copy link
Member

shoyer commented May 4, 2016

I don't think we want to add other units like months to Timedelta without a full overhauling of time units.

I also don't think we want to add a new PeriodDelta type.

So I guess that leaves returns DateOffsets. Are there any differences of Periods that could not be represented with DateOffsets? The other reasonable choice is to simply raise a TypeError.

@jreback
Copy link
Contributor

jreback commented May 4, 2016

I don't think we want to add other units like months to Timedelta without a full overhauling of time units.

I am not sure this will work, but what is the issue here

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.20.0, 0.19.0 Jul 8, 2016
@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Aug 21, 2016

I don't think we want to add other units like months to Timedelta without a full overhauling of time units.

I am not sure this will work, but what is the issue here

A month has not a well defined length, so does not fit in the current concept of Timedelta's.

@sinhrks Do you want to do this for 0.19? (changing to DateOffset, if possible) Or push to next release?

@dhirschfeld
Copy link
Contributor

I'm not sure I understand what the problem is here - I like the fact that it returns an int. You know what the implied freq is anyway so I don't see much of an issue with losing the freq info.

In [8]: p0 = pd.Period('2011-01', freq='M')
   ...: p1 = pd.Period('2011-03', freq='M')

In [9]: dt = p1 - p0

In [10]: assert p1 == p0 + dt

I suppose there's a type-safety argument to be made in that you could prevent a PeriodDelta('2M') from being added to a PeriodIndex of a different frequency, but I just don't see that ever really happening in practice and whatever benefit that brings has to be weighted against the increased api size and maintenance burden.

FWIW the old scikits.timeseries simply returned ints for the difference and that worked well in practice... for me at least.

@dhirschfeld
Copy link
Contributor

It would also be nice if you could subtract PeriodIndexes and get an array of ints:

In [14]: periods = pd.period_range('01-Jan-2017', '01-Jan-2020', freq='Q')

In [15]: periods - periods
Traceback (most recent call last):

  File "<ipython-input-15-5a6917b72e26>", line 1, in <module>
    periods - periods

  File "C:\Python\lib\site-packages\pandas\tseries\base.py", line 658, in __sub__
    typ2=type(other).__name__))

TypeError: cannot subtract PeriodIndex and PeriodIndex

@jreback
Copy link
Contributor

jreback commented Jan 9, 2017

@dhirschfeld as indicated above, returning an array (or scalar) of DateOffsets is the only solution here. Integers are the result of the implementation leaking thru here.

@jreback jreback modified the milestones: Next Major Release, 0.23.0 Mar 8, 2018
@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018
@jreback jreback modified the milestones: Next Major Release, 0.24.0 Jun 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Enhancement Period Period data type Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants