How to get day of month, week, year from date strings in pandas
In Python, the pandas library includes built-in functionalities that allow you to perform different tasks with only a few lines of code. One of these functionalities allows you to find out the day of the month, the week, the year, and the week number from date strings.
Representation
Input
01 Jan 2010
Output
Day of Week: "Friday"
Date: "1"
Day number of Year: "1"
Week number of Year: "53"
Method
#importing pandas and dateutil parse librariesimport pandas as pdfrom dateutil.parser import parse#initializing pandas seriesseries = pd.Series(['06 June 2021', '12-12-2001', '20160513', '2016/04/14', '2004-05-15', '2021-08-15T13:20'])#parsing and mapping date strings to seriesresult = series.map(lambda iterator: parse(iterator))#finding day of weekprint("Day of week: ", result.dt.weekday_name.tolist())#finding date of monthprint("Date: ", result.dt.day.tolist())#finding day number of yearprint("Day number of year: ", result.dt.dayofyear.tolist())#finding week number of yearprint("Week number of year: ", result.dt.weekofyear.tolist())