0

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?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
mchen
  • 9,808
  • 17
  • 72
  • 125
  • One important thing if you get an ``OSError`` is to include which ``OS`` you are using in your question. – MSeifert Feb 15 '16 at 20:01

1 Answers1

2

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. :-)

MSeifert
  • 145,886
  • 38
  • 333
  • 352