Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
pandas
date
datestring
communitycreator

How to get day of month, week, year from date strings in pandas

Arslan Bajwa

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 libraries
import pandas as pd
from dateutil.parser import parse

#initializing pandas series
series = 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 series
result = series.map(lambda iterator: parse(iterator))

#finding day of week
print("Day of week: ", result.dt.weekday_name.tolist())

#finding date of month
print("Date: ", result.dt.day.tolist())

#finding day number of year
print("Day number of year: ", result.dt.dayofyear.tolist())

#finding week number of year
print("Week number of year: ", result.dt.weekofyear.tolist())

RELATED TAGS

python
pandas
date
datestring
communitycreator
RELATED COURSES

View all Courses

Keep Exploring