You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeError when comparing TimeDelta and a nan Quantity #15230
Running on python 3.11, comparing a Quantity holding a nan value to a TimeDelta object throws a TypeError rather than returning False, as expected. (see code in 'How to reproduce'.
I would expect a Quantity with a value of nan but a time unit to compare just like comparing raw numbers, returning False
Expected behavior
I would expect this to return False, similar to other comparisons with np.nan
How to Reproduce
Install astropy
open an interactive python shell.
>>> from astropy.time import TimeDelta
>>> import astropy.units as u
>>> import numpy as np
>>> TimeDelta(5, format="sec") < (np.nan * u.s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'TimeDelta' and 'Quantity'
>>>
>>> TimeDelta(5, format="sec") < u.Quantity(np.nan, unit=u.s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'TimeDelta' and 'Quantity'
Welcome to Astropy 👋 and thank you for your first issue!
A project member will respond to you as soon as possible; in the meantime, please double-check the guidelines for submitting issues and make sure you've provided the requested details.
GitHub issues in the Astropy repository are used to track bug reports and feature requests; If your issue poses a question about how to use Astropy, please instead raise your question in the Astropy Discourse user forum and close this issue.
If you feel that this issue has not been responded to in a timely manner, please send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail [email protected].
Thanks for reporting! I agree it would be nice if this just worked!
The underlying problem is that in the comparison, TimeDelta tries to turn the quantity into its own type, i.e., TimeDelta(np.nan * u.s), and this fails because TimeDelta does not support non-finite numbers. This reminded me there is a long-standing issue about this : #6509 (though that is in part also about a wish to have masked times).
Note that I do think we need something like this to generically introduce the behaviour that all comparisons are falls with it. It would be equivalent to "Not a Time" (NaT) that numpy datetime has.
I have been thinking a bit more too, and in principle, I think in astropy/time/formats.py, one could override _check_val_type for TimeDeltaNumeric and not do the test whether the values are finite. In contrast to Time, for TimeDelta NaN and infinity are fairly well defined. Not trivial, but also not too deep in the innards.
Activity
github-actions commentedon Aug 26, 2023
Welcome to Astropy 👋 and thank you for your first issue!
A project member will respond to you as soon as possible; in the meantime, please double-check the guidelines for submitting issues and make sure you've provided the requested details.
GitHub issues in the Astropy repository are used to track bug reports and feature requests; If your issue poses a question about how to use Astropy, please instead raise your question in the Astropy Discourse user forum and close this issue.
If you feel that this issue has not been responded to in a timely manner, please send a message directly to the development mailing list. If the issue is urgent or sensitive in nature (e.g., a security vulnerability) please send an e-mail directly to the private e-mail [email protected].
mhvk commentedon Aug 26, 2023
Thanks for reporting! I agree it would be nice if this just worked!
The underlying problem is that in the comparison,
TimeDeltatries to turn the quantity into its own type, i.e.,TimeDelta(np.nan * u.s), and this fails becauseTimeDeltadoes not support non-finite numbers. This reminded me there is a long-standing issue about this : #6509 (though that is in part also about a wish to have masked times).Note that I do think we need something like this to generically introduce the behaviour that all comparisons are falls with it. It would be equivalent to "Not a Time" (NaT) that numpy datetime has.
pllim commentedon Aug 26, 2023
So this is more of a feature request than a bug?
mhvk commentedon Aug 26, 2023
I guess it is a bug that can be solved with a new feature...
greyskyy commentedon Aug 28, 2023
i had hopes that it was something simple and I could whip out a quick PR ... but alas. It seems more involved. Sounds like a feature request to me.
mhvk commentedon Aug 28, 2023
I have been thinking a bit more too, and in principle, I think in
astropy/time/formats.py, one could override_check_val_typeforTimeDeltaNumericand not do the test whether the values are finite. In contrast toTime, forTimeDeltaNaN and infinity are fairly well defined. Not trivial, but also not too deep in the innards.