Reputation: 442
I have a pandas dataframe which has dates in the format YYYYMMDD stored as a float32. Is it possible to somehow calculate what day of the week each day is? Eg. monday = 1, tuesday =2, etc?
I don't even know where to start.
Upvotes: 0
Views: 804
Reputation: 9941
We can convert the column to str
first, then to datetime
and use .dt.weekday
:
df = pd.DataFrame({'date': [20210101, 20210102]})
df['weekday'] = pd.to_datetime(df['date'].astype(str)).dt.weekday
df
Output:
date weekday
0 20210101 4
1 20210102 5
P.S. Here Monday = 0, ..., Sunday = 6, so if you want 1-7 instead, you can add 1 to column values
Upvotes: 1