Skip to content

TypeError when comparing TimeDelta and a nan Quantity #15230

Closed

Description

@greyskyy

Description

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

  1. Install astropy
  2. 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'

Versions

import platform; print(platform.platform())
import sys; print("Python", sys.version)
import astropy; print("astropy", astropy.version)
import numpy; print("Numpy", numpy.version)
import erfa; print("pyerfa", erfa.version)
import scipy; print("Scipy", scipy.version)
import matplotlib; print("Matplotlib", matplotlib.version)

Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.31
Python 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]
astropy 5.2.1
Numpy 1.24.1
pyerfa 2.0.0.1
Scipy 1.10.0
Matplotlib 3.6.3

Activity

  1. github-actions commented on Aug 26, 2023

    @github-actions
    Contributor

    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].

  2. mhvk commented on Aug 26, 2023

    @mhvk
    Contributor

    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.

  3. pllim commented on Aug 26, 2023

    @pllim
    Member

    So this is more of a feature request than a bug?

  4. mhvk commented on Aug 26, 2023

    @mhvk
    Contributor

    I guess it is a bug that can be solved with a new feature...

  5. greyskyy commented on Aug 28, 2023

    @greyskyy
    Author

    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.

  6. mhvk commented on Aug 28, 2023

    @mhvk
    Contributor

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions