Pandas DataFrame | asfreq method
Start your free 7-days trial now!
Pandas DataFrame.asfreq(~) method returns a DataFrame with a new time index that respects the specified frequency.
Parameters
1. freqlink | DateOffset or string
The frequency used to create a new time index.
2. methodlink | None or string | optional
The method by which to fill the resulting NaNs:
Value | Description |
|---|---|
or
| Fill with the next valid observation. |
| Fill with the previous valid observation. |
| Leave |
By default, method=None.
3. normalizelink | boolean | optional
Whether or not to set the time of day to midnight. By default, normalize=False.
4. fill_valuelink | scalar | optional
The value to fill missing values with. Note that missing values that existed in the source DataFrame will not be filled - only those that arise due to this method will be filled. By default, fill_value=None.
Return Value
A DataFrame with a new time index that respects the specified frequency.
Examples
Consider the following DataFrame:
df
A B2020-12-25 00:00:00 1 42020-12-25 00:01:00 2 52020-12-25 00:02:00 3 6
Basic usage
To set a new time index with a frequency of 30 seconds:
df.asfreq(freq="30S")
A B2020-12-25 00:00:00 1.0 4.02020-12-25 00:00:30 NaN NaN2020-12-25 00:01:00 2.0 5.02020-12-25 00:01:30 NaN NaN2020-12-25 00:02:00 3.0 6.0
The reason why we get NaN for some rows is that their index is not present in the source DataFrame df. For instance, we get NaN for the second row because df does not contain a row with index 2020-12-25 00:00:30.
Filling methods
None
By default, method=None, which means that new values will be marked as NaN and no filling rules will be applied:
df.asfreq(freq="30S") # method=None
A B2020-12-25 00:00:00 1.0 4.02020-12-25 00:00:30 NaN NaN2020-12-25 00:01:00 2.0 5.02020-12-25 00:01:30 NaN NaN2020-12-25 00:02:00 3.0 6.0
ffill
To fill using the previous valid observation:
df.asfreq(freq="30S", method="ffill")
A B2020-12-25 00:00:00 1 42020-12-25 00:00:30 1 42020-12-25 00:01:00 2 52020-12-25 00:01:30 2 52020-12-25 00:02:00 3 6
bfill
To fill using the next valid observation:
df.asfreq(freq="30S", method="bfill")
A B2020-12-25 00:00:00 1 42020-12-25 00:00:30 2 52020-12-25 00:01:00 2 52020-12-25 00:01:30 3 62020-12-25 00:02:00 3 6
Normalising the index
Consider the following DataFrame:
df
A B2020-12-25 00:00:00 1 42020-12-25 00:01:00 2 52020-12-25 00:02:00 3 6
To reset the time of day to midnight, set normalize=True:
df.asfreq(freq="MIN", normalize=True)
A B2020-12-25 1 42020-12-25 2 52020-12-25 3 6
Specifying a fill value
Consider the same df as above:
df
A B2020-12-25 00:00:00 1 42020-12-25 00:01:00 2 52020-12-25 00:02:00 3 6
Instead of the default NaNs, we can choose the value to fill with using fill_value:
df.asfreq(freq="30S", fill_value=0)
A B2020-12-25 00:00:00 1 42020-12-25 00:00:30 0 02020-12-25 00:01:00 2 52020-12-25 00:01:30 0 02020-12-25 00:02:00 3 6
Case when NaNs already exist in DataFrame
Consider the following DataFrame:
df
A B2020-12-25 00:00:00 1 4.02020-12-25 00:01:00 2 5.02020-12-25 00:02:00 3 NaN
Here, our df has a NaN.
Calling the asfreq(~) method:
df.asfreq(freq="30S", fill_value=8)
A B2020-12-25 00:00:00 1 4.02020-12-25 00:00:30 8 8.02020-12-25 00:01:00 2 5.02020-12-25 00:01:30 8 8.02020-12-25 00:02:00 3 NaN
Notice how the original NaN is not filled. - only NaN that arise due to the method will be filled.