Skip to content

Series[offsets] + Series[datetime64] fails #19211

Closed
@jbrockmendel

Description

@jbrockmendel
Member
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

chris-b1 commented on Jan 12, 2018

@chris-b1
Contributor

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

chris-b1 commented on Jan 12, 2018

@chris-b1
Contributor

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.

In [220]: pd.Index(other) + ser
C:\Users\chris.bartak\AppData\Local\Continuum\Anaconda3\envs\py36\lib\site-packages\pandas\core\ops.py:556: PerformanceWarning: Adding/subtracting array of DateOffsets to Series not vectorized
  "Series not vectorized", PerformanceWarning)
Out[220]: DatetimeIndex(['2001-01-01', '2000-02-29'], dtype='datetime64[ns]', freq=None)
jbrockmendel

jbrockmendel commented on Jan 12, 2018

@jbrockmendel
MemberAuthor

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

mroeschke commented on Apr 1, 2020

@mroeschke
Member

Looks like this works now on master Could use a test

In [32]: ser = pd.Series([pd.Timestamp('2000-1-1'), pd.Timestamp('2000-2-1')])
    ...: other = pd.Series([pd.offsets.DateOffset(years=1), pd.offsets.MonthEnd()])

In [34]: other + ser
/Users/matthewroeschke/pandas-mroeschke/pandas/core/arrays/datetimelike.py:1305: PerformanceWarning: Adding/subtracting array of DateOffsets to DatetimeArray not vectorized
  PerformanceWarning,
Out[34]:
0   2001-01-01
1   2000-02-29
dtype: datetime64[ns]

In [37]: pd.__version__
Out[37]: '1.1.0.dev0+1068.g49bc8d8c9'

10 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @jreback@chris-b1@jbrockmendel@mroeschke

      Issue actions

        Series[offsets] + Series[datetime64] fails · Issue #19211 · pandas-dev/pandas