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. In this recipe, we will extract different time parts of a datetime
variable by utilizing pandas and NumPy.
Getting ready
The following are some of the features that we can extract offtheshelf using pandas:
pandas.Series.dt.hour
pandas.Series.dt.minute
pandas.Series.dt.second
How to do it...
To proceed with this recipe, we must import the necessary libraries and create a toy dataset:
- Let’s import
pandas
andnumpy
:import numpy as np import pandas as pd
- Let’s create 20
datetime
observations, beginning from2019-03-05
at midnight followed by increments...