Series[datetime64] - PeriodIndex vs DatetimeIndex - PeriodIndex · Issue #18850 · pandas-dev/pandas (original) (raw)

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