pythonnumpytimedeltadatetime64

Python numpy: don't understand the result of subtracting np.datetime64 dates


If I do the following operation:

import numpy as np

np.datetime64('20230901') - np.datetime64('20230831')

I get the result

numpy.timedelta64(70,'Y')

I get that the letter 'Y' in this case means Year, but what does the 70 mean?

Furthermore, if I do the same operation specifying a time:

import numpy as np

np.datetime64('20230901-01') - np.datetime64('19700101-01')

I get the same number but in months:

numpy.timedelta64(840,'M')

What is the meaning of that amount of years/months?


Solution

  • numpy.datetime64 takes the YYYY-MM-DD format as input if you want to specify year-month-day, the dashes are important:

    np.datetime64('2023-09-01') - np.datetime64('2023-08-31')
    

    Output: numpy.timedelta64(1,'D')

    Without them, the strings are interpreted as years, effectively having 20230901-20230831 = 70 years.