Finding day of the week for a datetime64
I think you could do in this way:
import numpy as np
import datetime
t = '2018-09-19'
t = np.datetime64(t)
day = t.astype(datetime.datetime).isoweekday()
print day
Output:
3
Please look at the following related SO question:
Get weekday/day-of-week for Datetime column of DataFrame
I understand that your data is a numpy array, it may be useful to convert your data into a dataframe format via pandas. This is shown below.
import pandas as pd
df = pd.DataFrame(numpyArray)
Provided your data is now in a dataframe, then the below code (taken from the above link), should provide you with the weekday you are after.
df['weekday'] = df['Timestamp'].dt.dayofweek
Additionally this reference may also assist you.
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.dayofweek.html
If you're looking for a genuinely vectorized numpy approach, you could do:
def day_of_week_num(dts):
return (dts.astype('datetime64[D]').view('int64') - 4) % 7
It's a bit hacky, and just takes advantage of the fact that numpy.datetime64
s are relative to the unix epoch, which was a Thursday.
(I don't know if this is an implementational detail that could change without notice, but you can always check with assert np.zeros(1).astype('datetime64[D]') == np.datetime64('1970-01-01', 'D')
.)