Closed
Description
ser = pd.Series([pd.Timestamp('2000-1-1'), pd.Timestamp('2000-2-1')])
other = pd.Series([pd.offsets.DateOffset(years=1), pd.offsets.MonthEnd()])
>>> ser + other
0 2001-01-01
1 2000-02-29
dtype: datetime64[ns]
>>> other + ser
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pandas/core/ops.py", line 698, in wrapper
result = wrap_results(safe_na_op(lvalues, rvalues))
File "pandas/core/ops.py", line 665, in safe_na_op
lambda x: op(x, rvalues))
File "pandas/_libs/algos_common_helper.pxi", line 1212, in pandas._libs.algos.arrmap_object
result[i] = func(index[i])
File "pandas/core/ops.py", line 665, in <lambda>
lambda x: op(x, rvalues))
File "pandas/tseries/offsets.py", line 352, in __add__
return self.apply(other)
File "pandas/tseries/offsets.py", line 77, in wrapper
result = func(self, other)
File "pandas/tseries/offsets.py", line 212, in apply
other = other + self._offset
TypeError: ufunc add cannot use operands with types dtype('<M8[ns]') and dtype('O')
pd.Index(other) + ser
is OK, as is other + pd.DatetimeIndex(ser)
Activity
chris-b1 commentedon Jan 12, 2018
More fundamental than the arithmetic is that Series/arrays of offsets aren't meaningfully defined as a type, compared to, e.g.
Timedelta
.It wouldn't be an unreasonable addition, something that would have occasionally been useful to me, but a pretty large change. Probably less painful after #19174, et al.
chris-b1 commentedon Jan 12, 2018
As you noted, this does actually sort of work (surprise to me!). I suppose may not need to be that invasive, could just involve the dispatch for
+
on object Series.jbrockmendel commentedon Jan 12, 2018
The direction we’re going with series arithmetic is to dispatch to the relevant Index subclasses (#19024, #18824) implementations, since they handle type checking (and timezones) much more carefully than the current Series implementations.
mroeschke commentedon Apr 1, 2020
Looks like this works now on master Could use a test
10 remaining items