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

Comparison between 'Timestamp' with type '_PInf' #5

Closed
gfitas opened this issue Nov 29, 2018 · 7 comments
Closed

Comparison between 'Timestamp' with type '_PInf' #5

gfitas opened this issue Nov 29, 2018 · 7 comments
Labels
bug Something isn't working

Comments

@gfitas
Copy link

gfitas commented Nov 29, 2018

Hi,
First off all, an amazing thank you for this library

I would like to subtract two intervals of panda's timestamp.
But it seems to crash when it tries to compare infinite value with a timestamp

a = I.closed(Timestamp('2018-11-11 09:00:00'),Timestamp('2018-11-11 17:00:00'))
b = I.closed(Timestamp('2018-11-11 10:00:00'),Timestamp('2018-11-11 12:00:00'))
c = a - b

Here is the traceback

File "\lib\site-packages\intervals.py", line 753, in __sub__
    return self & ~other
File "\lib\site-packages\intervals.py", line 745, in __invert__
    complements = [~i for i in self._intervals]
File "\lib\site-packages\intervals.py", line 745, in <listcomp>
    complements = [~i for i in self._intervals]
File "\lib\site-packages\intervals.py", line 432, in __invert__
    AtomicInterval(inverted_right, self._upper, inf, OPEN)
File "\lib\site-packages\intervals.py", line 242, in __init__
    if self.is_empty():
File "\lib\site-packages\intervals.py", line 283, in is_empty
    self._lower > self._upper or
File "pandas\_libs\tslibs\timestamps.pyx", line 170, in pandas._libs.tslibs.timestamps._Timestamp.__richcmp__
TypeError: Cannot compare type 'Timestamp' with type '_PInf'

Thank you in advance

@AlexandreDecan
Copy link
Owner

AlexandreDecan commented Nov 29, 2018

Hello,

I think the problem is that pandas._libs.tslibs.timestamps._Timestamp.__richcmp__ raises a TypeError for unsupported types instead of returning a NotImplemented, as observed here: https://github.com/pandas-dev/pandas/blob/master/pandas/_libs/tslibs/timestamps.pyx#L247. The thing is I don't know if there is something specific for __richcmp__ in Cython. I will have to check.

Because of this, the comparison stops as soon as the TypeError is raised, instead of calling the operator on the other object (_PInf) that supports it (see, e.g., https://docs.python.org/3/library/constants.html#NotImplemented).

I don't see any easy workaround, and I don't even know if there is one :-) I will dig a little bit, and keep you in touch.

@AlexandreDecan AlexandreDecan added the bug Something isn't working label Nov 29, 2018
@AlexandreDecan
Copy link
Owner

Even in Cython, rich comparisons should return a NotImplemented object, and not raising a TypeError (according to my understanding of https://cython.readthedocs.io/en/latest/src/userguide/special_methods.html).

I don't think it will be easy to submit a patch for tslibs... I will try to find a workaround.

@AlexandreDecan
Copy link
Owner

@gfitas As a temporary workaround, I suggest you to convert your Timestamp objects to something that properly implements rich comparisons the "Python way" (i.e. by delegating unsupported types to the other operand). A naive solution would be to convert them to Python datetime objects by mean of the to_pydatetime method.

@AlexandreDecan
Copy link
Owner

I close this issue, as there is nothing I can do in python-intervals to fix it.
I hope a solution will be found in pandas.

@gfitas
Copy link
Author

gfitas commented Dec 4, 2018

Thank you for your time,
I converted it in integer and it works perfectly.

Once again, thank you for your work

@AlexandreDecan
Copy link
Owner

FYI, the change in pandas is delayed to release 0.25.

@AlexandreDecan
Copy link
Owner

For information, pandas 0.25 was released two months ago, and includes the aforementioned fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants