Skip to content

Cannot compare timestamp against infinite #14090

Open

Description

@david-cortes

Since there is no absolute minimum for timestamp, if I want to make a comparison that would always return more/less, my first thought is to use infinite, which works for pretty much every other dtype (and makes for clear loop procedure initialization):

import numpy as np
np.datetime64("2019-01-01") > (-np.inf)
TypeError                                 Traceback (most recent call last)
<ipython-input-6-df4bfdeeac74> in <module>
----> 1 np.datetime64("2019-01-01") > (-np.inf)

TypeError: invalid type promotion

Contrast with R:

as.Date("2019-01-01") > (-Inf)
TRUE
as.POSIXct("2019-01-01") > (-Inf)
TRUE

Activity

  1. seberg commented on Jul 23, 2019

    @seberg
    Member

    Casting floats to datetimes is currently not really supported. You could support this comparison, but I am not sure I feel it is a good idea. What you really want seems more like getting the largest/smallest possible date. I can tell you that those are np.datetime64(np.iinfo(np.int64).max, "unit") and np.datetime64(np.iinfo(np.int64).min + 1, "unit"), but that is not ideal either I guess.

    Anyway, discussion welcome, but just defining comparison to floats might have its own caveats.

  2. david-cortes commented on Jul 24, 2019

    @david-cortes
    ContributorAuthor

    Casting floats to datetimes is currently not really supported. You could support this comparison, but I am not sure I feel it is a good idea. What you really want seems more like getting the largest/smallest possible date. I can tell you that those are np.datetime64(np.iinfo(np.int64).max, "unit") and np.datetime64(np.iinfo(np.int64).min + 1, "unit"), but that is not ideal either I guess.

    Anyway, discussion welcome, but just defining comparison to floats might have its own caveats.

    But I get

    np.datetime64(np.iinfo(np.int64).min + 1, "s")
    numpy.datetime64('292277026596-12-03T08:29:54')
    

    Although it at least compares properly:

    np.datetime64('2019-01-01') > np.datetime64(np.iinfo(np.int64).min + 1, "s")
    True
    
  3. david-cortes commented on Jul 24, 2019

    @david-cortes
    ContributorAuthor

    Also IMO would be nice to be able to do this:

    np.datetime64(np.nan)

    Like in R:

    as.POSIXct(NA)
    as.Date(NA)
  4. venaturum commented on Oct 14, 2021

    @venaturum

    +1 on introducing an "infinity" for datetime and timedelta.

    My desire for it stems from wanting to be able to construct infinite intervals in pandas.

    Eg this is a perfectly reasonable thing to do in pandas:

    >>> import pandas as pd
    
    >>> pd.Interval(0, np.inf).overlaps(pd.Interval(-10,10))
    True
    

    This infinite type would display as "inf" much like numpy.inf, and be used in arrays while preserving the dtype.

    Intervals can also be defined over their datetime and timedelta domains, but the above calculations cannot be done while there is no infinite equivalent for these domains.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions