0

I have two numpy arrays that contain dates of type datetime64 and I want to iterate through them and find the date differences?

import numpy

print(dates1)
array(['2019-10-10T21:59:17.074007', '2015-10-13T00:55:55.544607',
   '2017-05-24T06:00:15.959202', '2015-08-14T04:54:07.114346',
   '2018-05-04T05:45:18.072174', '2017-06-25T22:11:55.785210'],
  dtype='datetime64[us]')

print(dates2)
 array(['2015-10-28T00:55:55.544607', '2017-07-23T06:00:15.959202',
       '2019-07-13T11:16:44.130063', '2016-09-11T22:04:02.012082',
       '2017-09-26T21:22:14.873617', '2018-06-03T05:45:18.072174'],
      dtype='datetime64[us]')

I am thinking that they need to be ordered first (like from the earliest to the latest) but I am not very sure.

Braiam
  • 1
  • 11
  • 47
  • 78
Skarl001
  • 92
  • 1
  • 9
  • 1
    Please [edit] your post and add the expceted output ;) – azro Apr 17 '21 at 11:48
  • Does this answer your question? [Date difference in days and compare the result in Python](https://stackoverflow.com/questions/44901200/date-difference-in-days-and-compare-the-result-in-python) – youssef jallouli Apr 17 '21 at 11:48

1 Answers1

1

the np.datetime64 module can meet your needs. Below is sample code and result.

I don't know much about the subject but I think it will work.

The code:

for index in range(len(dates1)):
    print(np.datetime64(dates1[index])-np.datetime64(dates2[index]))

Response:

124751001529400 microseconds
-56091860414595 microseconds
-67410988170861 microseconds
-34103394897736 microseconds
18951783198557 microseconds
-29576002286964 microseconds