0

I want to find the maximum np.datetime64[ns] (so that in my algorithm min() never chooses it). I've tried the suggestion from What is the maximum timestamp numpy.datetime64 can handle? but this gives strange (very wrong!) results in nanosecond resolution:

>>> from datetime import datetime
>>> import numpy as np
>>> np.datetime64(datetime.max, "ns")
numpy.datetime64('1816-03-30T05:56:08.066276376')

I assume this is because datetime.max is a later date than the maximum np.datetime64[ns], so it wraps when converting.

Edit: I've found np.datetime64((1 << 63) - 1, 'ns') works (I assume that is the maximum), but is obviously gross. Is there a nicer way to construct this?

  • What's that algorithm of yours where you think you need this? – Manuel Apr 19 '21 at 12:41
  • @Manuel In a 3rd party library, there is a dict that maps string keys to deques of datetimes, accessed via a function that looks like: `return min(dict_of_deques, key=lookup)` where `lookup` is a user-defined function. I want to get the key corresponding to the deque with the earliest datetime at position zero. This is straightforward enough: `lookup = lambda key: dict_of_deques[key][0]`. The problem is when some of the queues are empty, we need to skip them so we don't get an index error. But we need to return something that won't affect the `min()` result, which leads to my question. – thegnarwhals Apr 19 '21 at 16:12
  • Ah, ok. And that `min` call isn't yours? Looks like a decent case for that then. Still, I might instead do `lookup = lambda key: (0, dict_of_deques[key][0]) if dict_of_deques[key] else (1,)`. – Manuel Apr 19 '21 at 18:02
  • Yeah the `min` is in the 3rd party library, it was designed for infinite data streams but I need to use it on finite data files, hence the lack of support for getting to the end of the queue! Ah that's nicer, and it works perfectly. I will use that. Thank you! – thegnarwhals Apr 20 '21 at 08:39

0 Answers0