BUG: DatetimeIndex + arraylike of DateOffsets by jbrockmendel · Pull Request #18849 · pandas-dev/pandas (original) (raw)

>>> dti = pd.date_range('2017-01-01', periods=2)
>>> other = np.array([pd.offsets.MonthEnd(), pd.offsets.Day(n=2)])

>>> dti + other
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O')

# Same for `dti - other`, `dti + pd.Index(other)`, `dti - pd.Index(other)`

>>> dti + pd.Series(other)
0    DatetimeIndex(['2017-01-31', '2017-01-31'], dt...
1    DatetimeIndex(['2017-01-03', '2017-01-04'], dt...
dtype: object

# yikes.
>>> dti + other
pandas/core/indexes/datetimelike.py:677: PerformanceWarning: Adding/subtracting array of DateOffsets to <class 'pandas.core.indexes.datetimes.DatetimeIndex'> not vectorized
  PerformanceWarning)
DatetimeIndex(['2017-01-31', '2017-01-04'], dtype='datetime64[ns]', freq=None)

>>> dti - pd.Index(other)
DatetimeIndex(['2016-12-31', '2016-12-31'], dtype='datetime64[ns]', freq=None)

>>> dti + pd.Series(other)
0   2017-01-31
1   2017-01-04
dtype: datetime64[ns]

Caveat This will need a follow-up to make sure name attribute is propogated correctly.