Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading