Given a particular date, it is possible to obtain the day of the week on which the date falls. This is achieved with the help of Pandas library and the to_datetime() method present in pandas.
In most of the datasets the Date column appears to be of the data type String, which definitely isn’t comfortable to perform any calculations with that column, such as the difference in months between 2 dates, the difference in time or finding the day of the week. Hence Pandas provides a method called
to_datetime() to convert strings into Timestamp objects.
We'll use the date format 'dd/mm/yyyy' Input : '24/07/2020' Output : 'Friday' Input : '01/01/2001' Output : 'Monday'
Once we convert a date in string format into a date time object, it is easy to get the day of the week using the method
day_name() on the Timestamp object created.
Example 1 : In this example, we pass a random date with the type ‘str’ and format ‘dd/mm/yyyy’ to the
to_datetime() method . As a result we get a Timestamp pandas object. Then we retrieve the day of the week by the Timestamp class’s
Example 2 : In the below example, dates of different formats have been converted to a Timestamp object and then their respective days of the week are retrieved using
day_name() method .
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course