Date Time Functionality in Python and Pandas

Jennifer Arty
3 min readAug 28, 2018

--

Before learning about datetime functions in Python and Pandas, working with dates was always a tedious process. On projects orassignments I either had to create a new columns to separate the month date and year to sort, or try to make it into a numerical value. Using datetime has made working with these data types a much easier process!

One way of creating a DateTime datatype is by using the datetime feature in python. Importing datetime you can can strptime() method, which will convert the string to a date time based on the format of the string. Below I have shown 4 examples of how different types of date formats will convert to DateTime. By default, once a date has been converted it will come out as:

Year-Month-Day Hour:Minute:Seconds

In order for the string to come out in this format, you need to tell the function what information you are passing through using strtime directive. The format of the directives need to match the format of the datetime string that is being passed through as well as the special chracters.

For example : Date1 is passing through day of week, Month day and year written out. To account for this we use

%A =weekday,

%B =Month,

%d = day

%Y= 4-digit year

When calling type on the dt1 variable, we see that it was converted from a string to a datetime data type!

Pandas also has a DateTime feature built in where you can convert objects or integers into a DateTime data type, which makes analyzing data with continues times a breeze. To convert the column in the data frame, you have to call .to_datetime() method in pandas. Below is an example of the conversion:

Using an old project on 311 calls in NYC, I was able to convert the date column into a datetime data type and run some analysis on it. With the date in an easy to use form, Pandas makes analyzing data by month, day, hour etc. a breeze. This is done by using Time/Date components for the different timestamps your are interested in.Taking a look at the complaint types of the 311 calls below for noise complaints, I was able to group data by month, hour and day of week. We see that most noise complaints come in the summer months when everyone is outside enjoying the warm weather. When taking a look at the hour, its expected to see the complaints coming in from 10pm to the most coming in at mid-night. Lastly, weekends are the days of week where most of the complaints are coming in.

Analysizng complaint calls by month, hour, and day of week

See below for list of different Time/Date Components that can be used

https://pandas.pydata.org/pandas-docs/stable/timeseries.html

Learning and using DateTime in python and pandas has made a daunting task of tackling dates and time objects a breeze! I am excited to continue to learn and explore all the possible functionality it has to offer from analyzing, visualizing and manipulating time series data.

--

--