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

DatetimeIndex + ndarray[int] wrong, reverse op errors #19123

Closed
jbrockmendel opened this issue Jan 7, 2018 · 6 comments
Closed

DatetimeIndex + ndarray[int] wrong, reverse op errors #19123

jbrockmendel opened this issue Jan 7, 2018 · 6 comments
Labels
Bug Datetime Datetime data dtype
Milestone

Comments

@jbrockmendel
Copy link
Member

In both 0.22.0 and 1265f66

dti = pd.date_range('2016-01-01', periods=3, freq='D')
tdi = pd.TimedeltaIndex(['1 day', '2days', '3 days'], freq='D')
arr = np.array([1, 2, 3], dtype=np.int64)

>>> dti + arr
DatetimeIndex(['2016-01-01', '2016-01-02', '2016-01-03'], dtype='datetime64[ns]', freq='86400000000001N')

>>> dti - arr
DatetimeIndex(['2015-12-31 23:59:59.999999999',
               '2016-01-01 23:59:59.999999998',
               '2016-01-02 23:59:59.999999997'],
              dtype='datetime64[ns]', freq='86399999999999N')

>>> arr + dti
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'DatetimeIndex'

>>> tdi + arr
TimedeltaIndex(['1 days 00:00:00.000000', '2 days 00:00:00.000000',
                '3 days 00:00:00.000000'],
               dtype='timedelta64[ns]', freq='86400000000001N')

>>> arr + tdi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'TimedeltaIndex'

>>> tdi - arr
TimedeltaIndex(['0 days 23:59:59.999999', '1 days 23:59:59.999999',
                '2 days 23:59:59.999999'],
               dtype='timedelta64[ns]', freq='86399999999999N')

>>> arr - tdi
TimedeltaIndex(['-1 days +00:00:00.000000', '-2 days +00:00:00.000000',
                '-3 days +00:00:00.000000'],
               dtype='timedelta64[ns]', freq='-86399999999999N')

Expected:

dti = pd.date_range('2016-01-01', periods=3, freq='D')
tdi = pd.TimedeltaIndex(['1 day', '2days', '3 days'], freq='D')
arr = np.array([1, 2, 3], dtype=np.int64)

>>> dti + arr
DatetimeIndex(['2016-01-02', '2016-01-04', '2016-01-06'], dtype='datetime64[ns]', freq='2D')

>>> arr + dti
DatetimeIndex(['2016-01-02', '2016-01-04', '2016-01-06'], dtype='datetime64[ns]', freq='2D')

>>> tdi + arr
TimedeltaIndex(['2 days', '4 days', '6 days'], dtype='timedelta64[ns]', freq='2D')

>>> arr + tdi
TimedeltaIndex(['2 days', '4 days', '6 days'], dtype='timedelta64[ns]', freq='2D')

>>> tdi - arr
TimedeltaIndex(['0 days', '0 days', '0 days'], dtype='timedelta64[ns]', freq=None)

>>> arr - tdi
TimedeltaIndex(['0 days', '0 days', '0 days'], dtype='timedelta64[ns]', freq=None)
@jreback
Copy link
Contributor

jreback commented Jan 10, 2018

what do you think about banning all integer arithmetic with DTI (wether has a freq or not) and TDI (already banned), (but leave for PI)?

I know its sometimes convenient but it relies much to heavily on the 'freq'. which is kind of a hack for DTI.

@jbrockmendel
Copy link
Member Author

It would have the upside of making Series[datetime64] and DatetimeIndex more interchangeable. We'd have to (get to) get rid of Timestamp integer arithmetic at the same time.

@jreback
Copy link
Contributor

jreback commented Jan 11, 2018

yeah I wrote this then erased it. It IS convenient.

@jbrockmendel
Copy link
Member Author

Hmm this also leads to a weird non-commutativity:

dti = pd.date_range('2016-01-01', periods=3, freq='D')
arr = np.array([1, 2, 3], dtype=np.int64)

>>> dti + arr
DatetimeIndex(['2016-01-02', '2016-01-04', '2016-01-06'], dtype='datetime64[ns]', freq='2D')
>>> (dti + arr) - arr
DatetimeIndex(['2015-12-31', '2015-12-31', '2015-12-31'], dtype='datetime64[ns]', freq=None)

Since dti+arr comes back with freq=2D, the subtraction operation doens't just undo the addition.

@jbrockmendel
Copy link
Member Author

This is fixed in master, not sure when

@jbrockmendel
Copy link
Member Author

Made a note in #18824 to track down what PR fixed this. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype
Projects
None yet
Development

No branches or pull requests

3 participants