Python Tutorial: Creating a DatetimeIndex

Learn how to combine separate date and time columns in pandas, convert them to datetime, and set that as your index. The tutorial uses str.replace and str.cat to standardize and merge the columns, then pd.to_datetime to convert and setindex to create a DatetimeIndex that lets you filter by date and plot time series easily.
Chapters
In the last exercise, you fixed the data type of the is arrested column. Now we're going to build a datetime index for our data frame. Let's take a look at the head of the data set again. As you can see, the date and time of each traffic stop are stored in separate columns, both of which are object columns. Because we'll be using stop, date and stop time in our analysis, we're going to combine these two columns into a single column and then convert it to Pandas datetime format. This will be beneficial because Unlike object columns, DateTime columns provide date based attributes that will make our analysis easier. Let's see an example of this. Using the Apple stock price data frame from the previous video, date and time are stored in separate columns. So so the first task is to combine these two columns using a string method. As you might remember from previous courses, string methods such as replace are series methods available via the STR accessor. In this example, we're replacing the forward slash in the date column with a dash. It outputs a new series in which the string replacement has been made, though this change is temporary since we haven't saved the new series anyway. To combine the columns, we're going to use the STR cat method, which is short for Concatenate. We'll concatenate the date column with the time column and tell Pandas to separate them with a space storing the result in a series object named Combined. You can see that the combined series contains both the date and time. It's still an object column, but it's now ready for conversion to DateTime format. To convert the combined series to DateTime format, you simply pass it to the todatetime function and store the result in a new column. We didn't even need to specify that the original data was in month, day, year format. Instead, Pandas just figured it out. Looking at the updated dataframe, you can see that the new column contains both the date and time and that it is stored in a more standard way. From the dtypes attribute, you can see that the new data type of the new column is datetime instead of object. One final step that we'll take is to set the datetime column as the index. That will make it easier to filter the dataframe by date, plot the data by date, and so on. We'll use the setindex method and specify that the operation should occur in place to avoid an assignment statement. You can see that the default index has been replaced with the datetime column and the index is now a special type called datetimeindex As a reminder, when an existing column becomes the index, it is no longer considered to be one of the dataframe columns. Now that you've seen how to create a datetime index for the Apple dataframe, you can practice these steps on our dataset of traffic stops. This is the final step before we begin our analysis of the dataset. In the next chapter.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







