pythondatetimenumpydatetime64

What is the maximum timestamp numpy.datetime64 can handle?


I'm trying to convert datetime to numpy.datetime64 but the following case fails:

>>> import numpy as np
>>> from datetime import datetime
>>> np.datetime64(datetime.max)
OSError: Failed to use 'localtime_s' to convert to a local time

I presume that datetime64 can't handle such far-dated timestamps.

So what is the maximum timestamp that datetime64 can handle?


Solution

  • Depends on what the specified unit of your np.datetime64 object is (according to the numpy docs). Since you have given a timestamp with microseconds the allowed range is [290301 BC, 294241 AD].

    This answered your question but I think the unspoken other question is why it throws an Exception:

    I'm facing the same error (using Windows) and I tried a=np.datetime64(datetime.max) which works. Therefore I suspect the problem is NOT the np.datetime64 span (because creating such a datetime works) but that the __repr__ requires the OS in some way and probably the OS limits it in your case. So check what's the maximum localtime of your OS and for every datetime after that you can still work with the np.datetime64 objects but cannot print them on screen. :-)