Working with DateTime in Python. Become a master of dates and times in... | by Sarah Beshr | Towards Data Science

Throughout your career as a data scientist, it’s inevitable that you’ll come across data containing date and time. For this reason, this extensive guide is here to help make manipulating date and time data using Python’s datetime module a breeze. We’ll cover a range of topics such as handling date and time strings, measuring time span, and working with datetime objects in pandas.
To introduce you to the datetime module, there are five main object classes that come in handy for different purposes. They are as follows:
datetime — allows us to manipulate dates and times together (month, day, year, hour, second, microsecond).
date— allows us to manipulate dates only (month, day, year).
time — you probably guessed it; this class allows us to manipulate time only (hour, minute, second, microsecond).
timedelta — used for measuring duration, the difference between two dates or times.
tzinfo — used for dealing with time zones. We won’t be covering this one in this tutorial.
Let’s go ahead and see when and how we can use those classes.
First things first, it’s important to note that datetime is both a module and a class within that module for when you’re writing your imports.
We’ll demonstrate two ways in which you can work with adatetime object.
First, you can pass the arguments to datetime starting with the largest time unit and ending with the smallest (year, month, day, hours, minutes, second). It’s worth mentioning that you won’t often come across this method in real life as you’ll most probably find yourself using readily available data, but it’s still good to know nonetheless.
Second, if you want to get the current datetime you can easily do so using the .now() function.
# import datetimeimport datetime from datetime# create datetime object # datetime_object1 = datetime(2021, 6, 1, 15, 23, 25)# get current datedatetime_object2 = datetime.now()
What’s cool about working with datetime is that we can do arithmetic on different datetime objects. For instance, let’s assume we want to find out the number of seconds elapsed between the previous two datetimes. We can do so as follows:
duration = datetime_object2 - datetime_object1duration.total_seconds()
You can access individual components of adatetime object using the datetime’s attributes; such as year, day, and hour attributes for instance.
# extract datadatetime_object2.yeardatetime_object2.day datetime_object2.hour
Further, you can also find the day of the week using the .weekday() function.
datetime_object2.weekday()
Running the previous line yields 2 as a result, which is a Wednesday. Note that Python counts weekdays from 0, starting on Monday. Meaning that:
0 = Monday
1 = Tuesday
2 = Wednesday
...
6 = Sunday
Now that we’ve covered the ins and outs of thedatetime object, you can also easily work with date and time objects as they use the same methods.
Thetimedelta class comes in handy in situations where we want to measure duration as it represents the amount of time between two dates or times. This is particularly useful when we want to add or subtract from dates or times.
We’ll go ahead and demonstrate how timedelta objects and datetimeobjects can be used together to do mathematical operations. For instance, we’ll add 27 days to the current time.
# import timedeltafrom datetime import timedelta# create a 27 day timedeltatd = timedelta(days=27)#add 27 days to current date27_days_later = datetime_object2 + td
Similar to what we did above, we can also use timedelta to subtract from the current date or any other date.
Note that timedelta can be used with any number of weeks, days, hours, minutes, etc. It can be used with time units as small as a microsecond or as large as 2.7 million years! That’s impressive.
Oftentimes, we’ll find that we want to convert objects from strings to datetime objects and vice versa. datetime includes two useful methods that can help us achieve this; namely, strptime() and strftime(). We can use strptime() to read strings containing date and time information and convert them to datetime objects, and strftime() to convert datetime objects back into strings.
When working withstrptime() we have to take into consideration that it can’t turn any string into a date and time, and therefore we’ll have to indicate the time format ourselves. This is best demonstrated with an example:
date_string = '2020-11-27'# Create date object with time format yyyy-mm-dddate = datetime.strptime(date_string, "%Y-%m-%d")
Note that strptime() takes two arguments:
string — time in string format
format — specific formatting of the time in the string
If you’re unfamiliar with the formatting codes required to help strptime() interpret our string input you can find a useful reference here.
pandas is one of the most popular Python modules, and it is so for good reasons. One of them is that it makes working with time series data a breeze. Similar to thedatetime module, pandas also has datetime and timedelta objects with similar functionality to that of the datetime module.
To give a quick introduction, we can convert datetime and duration strings into pandas Datetime objects using the following functions:
to_datetime() — converts dates and times in string format to Python datetime objects.
to_timedelta()— used for measuring duration.
These functions do a great job at converting strings to Python datetime as they detect the date’s format automatically, without requiring us to define it as we did for strptime().
Let’s examine this with a concrete example:
# import pandas import pandas as pd# create date object date = pd.to_datetime("4th of oct, 2020")
Even though our input was a seemingly complicated string, pandas is still able to correctly parse the string and return a datetime object.
Assume that we want to extract the month, hour, or minute from a date and store it as a new column inside a pandas dataframe. We easily do this using dt attributes.
For instance, we can use df['date'].dt.month to extract the month from a pandas column that includes the full date and store it inside a new column. This example can surely also be extended to different time units as well.
Similar to the datetime class that we worked with earlier, pandas is also capable of extracting elements such as the day of the week for instance. Once more, we use dt attributes to do this.
For example, we can use df['date'].dt.weekday to extract the number of the day, with 0 representing Monday, etc. In case we want to extract the name of the weekday straight away, we can conveniently use df['date'].dt.weekday_name for this.
In this guide, we’ve covered how you can successfully navigate the world of dates and times by handling strings, using timedelta to measure duration and do simple arithmetic as well as working with datetime objects in pandas.
If you have any questions, I’d be happy to answer them in the comments!
I hope that you found this guide helpful. If you did, you might find some of my other articles interesting as well: