A simple Datetime tutorial

Switching to Datetime Indexes in Python: A Simple Tutorial

Datetime is a tool used in Python programming for a multitude of different functions, ranging from programs for a specific time to programs for chronologically organizing data. It is a fundamental class of any Python programmer’s toolbox. Some form of Datetime is often used in everyday business data, such as recording transactions and historical information. Several companies have their data indexed by a simple numerical row number while also having a data column within their dataset. This article will explain why companies should switch to indexing by Datetime while also reviewing some simple tricks to try while using Datetime.

There are numerous reasons why a company should begin indexing with Datetime. For starters, it simplifies and organizes data in a chronological time structure. This allows for an easier and local method to pull company data. For example, say a store sells an array of different items and records each item sold, the time it was sold, and the price within its own index. If one were to organize the date with a Datetime index, they would immediately remove one column within the data set and easily sort the data chronologically. This enables the company to sort its transaction easier by a single date or dates, creating a more streamlined process in retrieving the data. Another benefit to using a Datetime index for data is how easy it is to convert data into Datetime and sort it by any measurable date.

Once a company decides to begin using Datetime as their main source of indexing their data, what is the first step? Assuming they are using Python, it is as simple as importing the Datetime library with a few lines of code.

Once the data is imported, the next step is to check what format the date column in the dataset is; generally, it is String. Then, the company must enter a line of code to change the column into a Datetime formatted column.

Above is how to change a column to a Datetime object with just one small issue ‘YOUR FORMAT HERE’. A unique thing about converting a string into a Datetime object is that one must input the string based on its format.

Datetime has several ‘codes’ that the library uses in order to read a string properly as a Datetime object. Some of the major Datetime time codes are: (%Y = years, %m = month, %d = date, %H = hours, %M = minutes). Using these ‘codes’, enter in the specific format for the dates formatting including the necessary backlashes, dashes and semi-colons, as seen above.

Once the data is in the format of datetime, the next step is to reindex the Panda’s data frame with Datetime, as seen above.

Congratulations! A regular data frame has now turned into a Datetime indexed data frame. This will allow the data to be sorted by ascending and descending dates, pull specific dates, and more. This will streamline the data pulling process and help better represent the data within graphs overtime. If you have any questions, feel free to reach out to me!

More information about Datetime codes can be found here.