Extracting features from time with pandas
Some events occur more often at certain times of the day – for example, fraudulent activity is more likely to occur during the night or early morning. Air pollutant concentration also changes with the time of the day, with peaks at rush hour when there are more vehicles on the streets. Therefore, deriving time features is extremely useful for data analysis and predictive modelling. In this recipe, we will extract different time parts of a datetime
variable by utilizing pandas
and NumPy.
Getting ready
We can extract hours, minutes, and seconds using the following pandas
’ datetime
properties:
pandas.Series.dt.hour
pandas.Series.dt.minute
pandas.Series.dt.second
How to do it...
In this recipe, we’ll extract the hour
, minute
, and second
part of a time
variable. Let’s begin by importing the libraries and creating a sample dataset:
- Let’s import
pandas
andnumpy
:import numpy...