Skip to content

Unable to compare timedelta64 to timedelta #5963

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

Closed
cancan101 opened this issue Jan 16, 2014 · 4 comments · Fixed by #8884
Closed

Unable to compare timedelta64 to timedelta #5963

cancan101 opened this issue Jan 16, 2014 · 4 comments · Fixed by #8884
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@cancan101
Copy link
Contributor

In [426]:
s = pd.Series([timedelta(days=1), timedelta(days=2)])
s > timedelta(days=1)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-426-9094b7c93677> in <module>()
      1 s = pd.Series([timedelta(days=1), timedelta(days=2)])
----> 2 s > timedelta(days=1)

/usr/local/lib/python2.7/dist-packages/pandas-0.13.0rc1_78_g142ca62-py2.7-linux-x86_64.egg/pandas/core/ops.pyc in wrapper(self, other)
    561 
    562             # scalars
--> 563             res = na_op(values, other)
    564             if np.isscalar(res):
    565                 raise TypeError('Could not compare %s type with Series'

/usr/local/lib/python2.7/dist-packages/pandas-0.13.0rc1_78_g142ca62-py2.7-linux-x86_64.egg/pandas/core/ops.pyc in na_op(x, y)
    528 
    529             try:
--> 530                 result = getattr(x, name)(y)
    531                 if result is NotImplemented:
    532                     raise TypeError("invalid type comparison")

TypeError: can't compare datetime.timedelta to long

The comparison does work for timedelta64:

In [45]: s > np.timedelta64(timedelta(days=1))
Out[45]: 
0    False
1     True
dtype: bool
@cancan101
Copy link
Contributor Author

This might be related: #5436

@jreback
Copy link
Contributor

jreback commented Jan 16, 2014

ok...this is legit...just need some coercion

can you update to show if timedelta64 works?

@dieterv77
Copy link
Contributor

I ran into the same issue with pandas master, though i get a different exception (maybe a difference in numpy version?):

import pandas as pd
import numpy as np
from datetime import timedelta
print np.__version__
s = pd.Series([timedelta(days=1), timedelta(days=2)])
s > timedelta(days=1)

prints

1.6.1
Traceback (most recent call last):
  File "bug.py", line 9, in <module>
    s > timedelta(days=1)
  File "/home/dvandenbussche/.local/lib/python2.7/site-packages/pandas-0.13.0_472_g9109812-py2.7-linux-x86_64.egg/pandas/core/ops.py", line 563, in wrapper
    res = na_op(values, other)
  File "/home/dvandenbussche/.local/lib/python2.7/site-packages/pandas-0.13.0_472_g9109812-py2.7-linux-x86_64.egg/pandas/core/ops.py", line 530, in na_op
    result = getattr(x, name)(y)
TypeError: ufunc 'greater' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule 'safe'

FWIW: this was working with 0.10.1.
@jreback, i'm not too well informed about all the different date related types, but if you think it is feasible/sensible to give me some instructions, i can try and take a look.
In the end, the code tries to call the comparison operator of a numpy array (with dtype timedelta64[ns]) with a datetime.timedelta type. When you say coercion is needed, are you talking about the numpy array or the datetime.timedelta?

Thanks!

@jreback
Copy link
Contributor

jreback commented Jan 30, 2014

@dieterv77

timedeltas have been steadily moving towards being kept as timedelta64[ns] < 10 I think were object arrays.

in any event if you want pretty much anything to work with timedeltas you need numpy >= 1.7

1.6.1 is barely supported (most tests are skipped) as it is so thoroughly broken (datetime64 in 1.6.1 at least work somewhat)

so welcome to take a look with 1.6.1 but its a nightmare (serieously)...

this issue is pretty straightforward in numpy >= 1.7

all of the timedelta conversions are done here: https://github.com/pydata/pandas/blob/master/pandas/core/ops.py#L243

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

Successfully merging a pull request may close this issue.

3 participants