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

Series[datetime64] - PeriodIndex vs DatetimeIndex - PeriodIndex #18850

Closed
jbrockmendel opened this issue Dec 19, 2017 · 3 comments · Fixed by #19024
Closed

Series[datetime64] - PeriodIndex vs DatetimeIndex - PeriodIndex #18850

jbrockmendel opened this issue Dec 19, 2017 · 3 comments · Fixed by #19024
Labels
API Design Datetime Datetime data dtype Period Period data type
Milestone

Comments

@jbrockmendel
Copy link
Member

PeriodIndex w/ Series is allowed, PeriodIndex w/ DatetimeIndex is not. Is this intentional? If not, which should be made to match the other?

>>> dti = pd.date_range('2016-01-01', periods=3)
>>> ser = pd.Series(dti)
>>> pi = dti.to_period()

>>> ser - pi
0   0 days
1   0 days
2   0 days
dtype: timedelta64[ns]

>>> dti - pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/indexes/datetimelike.py", line 724, in __sub__
    typ2=type(other).__name__))
TypeError: cannot subtract DatetimeIndex and PeriodIndex

>>> pi - ser
0   0 days
1   0 days
2   0 days
dtype: timedelta64[ns]

>>> pi - dti
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pandas/core/indexes/datetimelike.py", line 731, in __rsub__
    return -(self - other)
  File "pandas/core/indexes/datetimelike.py", line 724, in __sub__
    typ2=type(other).__name__))
TypeError: cannot subtract DatetimeIndex and PeriodIndex
@jbrockmendel
Copy link
Member Author

It looks like this is caused by a line in ops._TimeOp._convert_to_array

        elif inferred_type == 'integer':
            # py3 compat where dtype is 'm' but is an integer
            if values.dtype.kind == 'm':
                values = values.astype('timedelta64[ns]')
            elif isinstance(values, pd.PeriodIndex):
                values = values.to_timestamp().to_series()

@jbrockmendel
Copy link
Member Author

@jreback if this can be confirmed as being incorrect behavior for Series, then the remaining point of contention in #18960 can be rendered moot.

@jreback
Copy link
Contributor

jreback commented Dec 31, 2017

series w pi is wrong doesn’t make any sense

@jreback jreback added API Design Period Period data type Datetime Datetime data dtype labels Jan 1, 2018
@jreback jreback modified the milestones: Next Major Release, 0.23.0 Jan 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants