Converting date to weekday type in python



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




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


Depending on your data, you can also convert a entire DataFrame column to date. Check out pandas.to_datetime() -