This article is available in: 日本語
This article is a reminder of how to fill in data intervals for data that is spaced by dates and times.
To fill in the interval between dates and times, use the asfreq function in pandas.
You can try the code in this article in the following google colab
python code
As an example, we will deal with the following data (assume that the data is stored in a variable called df below).
The data is in 10-minute increments, but the data is not present in some places, and the time interval is empty.
The policy for filling in the data is
- Specify the time column (datetime) as index
- Using asfreq function in pandas
- Undo index
import pandas as pd
df_ = df.set_index('datetime')
df_ = df_.asfreq(freq='10min')
df_fill = df_.reset_index()
print(df_fill)
As described above, it is possible to fill in date and time data that have been left in between.
The arguments that can be specified for freq in the asfreq function are detailed in the official reference below.