pythonnumpydatetime64

Numpy datetime64 time units - conversion from string with more than 4 year digits


I was playing around with extreme values for dates and realized a strange behaviour:

# 1. normal date in nanoseconds
print(np.datetime64('2020-04-14T00:00:00.000000', 'ns'))
# 2. extreme year date in nanoseconds
print(np.datetime64('-32020-04-14T00:00:00.000000', 'ns'))
# 3. extreme year date as year-month-day
print(np.datetime64('-32020-04-14T00:00:00.000000', 'D'))

gives:

2020-04-14T00:00:00.000000000
1884-06-01T23:24:35.153993728
-32020-04-14

I was wondering why the middle conversion gives a completely wrong date/time?


Solution

  • Because a np.datetime64[ns] is a 64 bits integer number of nano seconds since 1970-01-01 00:00, so they can only represent dates in the [1678 AD, 2262 AD] range. All timestamps outside that range are folded into it.

    Reference : numpy manual on Datetimes