Converting date to weekday type in python

pandas
ipython
python

#1

Hello,
I have been doing bike sharing demand problem in kaggle using pandas in python. There we have datetime in the form 01-01-2011 00:00 how can I bulid a new column to map each date to its week day. For example:

01-01-2011 00:00 Mon
02-01-2011 00:00 Tue

Thanks


#2

Hello,

you can do above using datetime library

from datetime import datetime
x = datetime.strptime(‘01-01-2011 00:00’, ‘%d-%m-%Y %H:%M’).strftime(’%A’)

Here 01-01-2011 00:00 is in the form %d-%m-%Y %H:%M
d - date in decimal number
m - month in decimal number
Y - year in decimal number
H- for hours in 0-23 form
M - for minutes in 0-59 form
A - gives the weekdays

Similarly we can get the other variables like time by replacing A with H


#3

Depending on your data, you can also convert a entire DataFrame column to date. Check out pandas.to_datetime() - https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_datetime.html