I have this format of datetime in Python "2022-09-01 08:36:22", how can I get which weekday of it?
I also created this dictionary
day_of_week = { 1: "sunday", 2: "monday", 3: "tuesday", 4: "wednesday", 5: "thursday", 6: "friday", 7: "saturday" }
to get number and convert it to corresponding day.
Update : september_2022["started_at"].dt.dayofweek
when I used this code I can get integer for corresponding day, but I want to create new column and assign it days of week.
september_2022["day_of_week"] = day_of_week[september_2022["started_at"].dt.dayofweek]
and get this error : unhashable type: 'Series'
CodePudding user response:
Using the datetime library:
datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S").strftime("%A")
CodePudding user response:
You can use dt.day_name
for a vectorial conversion:
df = pd.DataFrame({'date': ['2022-09-01 08:36:22', '2022-09-03 08:36:22']})
pd.to_datetime(df['date']).dt.day_name()
output:
0 Thursday
1 Saturday
Name: date, dtype: object