Get a single datetime feature (such as day, hour, year) from a Numpy.datetime64 array

1.2k Views Asked by At

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

0

There are 0 best solutions below