Select Page

11.4 Working with Dates and Times in Python


Python offers several tools that are built for handling time-stamped information.  To get started, we will look at the datetime object that comes with the datetime package.  To demonstrate, we will use some data that was captured on July 4, 1976 — America’s 200th birthday.  We will pick the moment at which park attendance peaked that day, 3:17 p.m., to create our first datetime object.  

When we enter “july4” into our Colab Notebook, we can see that it recognizes that this object belongs to a time-based format.   We did not give hour and minute values to the function when we first created july4, so we see the two zeros at the end here — by default, those values are populated as zeros when no value for them is specified.   We can now add those values, along with second, in order to heighten the precision with which Python sees this datetime object:

This datetime object is understood by Python to represent a particular moment in time.  

The numpy package also recognizes a time-based type, known as datetime64.  First, we will enter some dates as a numpy array:  

Numpy was able to take this data into an array, but it does not realize yet that the values represent dates.  By specifying ‘datetime64’ as the datatype, as shown below, we are explicitly telling numpy that we want these values to be seen as dates. 

In the code below, we are giving the arange() function a start value, an end value, a step value, and we are indicating that we want each value to represent one date.  As a result, we are getting all of the dates in the month of July 1976.  

Now that we have started to build a foundation with the way that time-related data types are handled in numpy, we will import pandas in order to explore its handling of date-time objects. 

Using the date_range() function in Pandas, we will specify a start date, a number of periods, and a frequency.  This will give us all of the July dates with even less work than the numpy approach required:

Pandas has a specialized DatetimeIndex that it uses for time objects. The output shown above indicates that pandas sees these values as dates.  The freq= ‘D’ reference at the end tells us that pandas understands these values to be daily.