1

I am currently updating an script, which was written using datetime library

from datetime import datetime and timedelta

One of the cool features of datetime is the function that returns you a datetime feature (suchs as .day, .year, .hour). eg:

datetime(1860,01,01).year
>1980

datetime(1860,01,01).month
>1

Now I expect that given a numpy.datetime64 array I can extract the datetime feature I am interested. The question is: Is there a numpy object that returns an array of the requested feature?. For instance:

#Given an input np.datetime64 array:
arr = array(['1981-01-01T00:00:00.000000', '1981-01-02T00:00:00.000000',
             '1981-01-03T00:00:00.000000', '1981-01-04T00:00:00.000000',
             '1981-01-05T00:00:00.000000'], dtype='datetime64[us]')
# I am trying to get all the months:
arr.months
>np.array(['01', '01', '01', '01','01'])
arr.days
>np.array(['01', '02', '03', '04','05'])

This is critical for the rework of my script but I haven't found any solution yet, please help me :) Thanks a lot! Joues

joueswant
  • 136
  • 10
  • 2
    Does this answer helps? https://stackoverflow.com/a/60112823/3250423 – yibo Sep 12 '20 at 22:41
  • Thanks a lot, well this was what I thought about. But I am actually using datetime's lists currently. For me, moving to np.datetime64 was done in order to avoid costly loops. That's is why I would like to know if there is a numpy object or a numpy way to do so – joueswant Sep 12 '20 at 22:52
  • are you sure numpy datetime arrays give you a performance plus for datetime calculations? For sure they are the way to go with pure numeric stuff but from my experience, they're bit cumbersome when it comes to datetime. Btw. you could also consider a numpy array of datetime objects, if you e.g. just need the indexing capability. – FObersteiner Sep 13 '20 at 15:59

0 Answers0