numpy datetime64 get day 1

numpy datetime64 get day

dates = np.arange(np.datetime64('2000-01-01'), np.datetime64('2010-01-01'))
years = dates.astype('datetime64[Y]').astype(int) + 1970
months = dates.astype('datetime64[M]').astype(int) % 12 + 1
days = dates - dates.astype('datetime64[M]') + 1

Here is what the above code is Doing:
1. np.arange(np.datetime64(‘2000-01-01’), np.datetime64(‘2010-01-01’))
– creates an array of dates from 2000-01-01 to 2010-01-01
2. dates.astype(‘datetime64[Y]’).astype(int) + 1970
– converts the dates to the year, then converts the year to an int, then adds 1970 to the int
3. dates.astype(‘datetime64[M]’).astype(int) % 12 + 1
– converts the dates to the month, then converts the month to an int, then takes the modulus of the int by 12, then adds 1
4. dates – dates.astype(‘datetime64[M]’) + 1
– converts the dates to the month, then subtracts the dates from the converted dates, then adds 1

Similar Posts