Signup/Sign In
PUBLISHED ON: APRIL 26, 2021

Python pandas Series.asfreq() Method

In this tutorial, we will learn the python pandas Series.asfreq() method. This method is used to convert the Time Series to the specified frequency. Using this method we can fill in any missing values or null values.

The below shows the syntax of the Series.asfreq() method.

Syntax

Series.asfreq(freq, method=None, how=None, normalize=False, fill_value=None)

Parameters

freq: It represents the DateOffset or str. It is the frequency DateOffset or string.

method: It includes the ‘backfill’/’bfill’, ‘pad’/’ffill’, and the default value is None. iT indicates the method to use for filling holes in reindexed Series.

how: It indicates the ‘start’, ‘end’, and the default is end.

normalize: It represents the bool(True or False), and the default value is False. It indicates whether to reset the output index to midnight.

fill_value: It is scalar, and it is optional. It is the value used for missing values, applied during upsampling.

Example: Converting the TimeSeries using the Series.asfreq() method

Let's create a TimeSeries and convert it to the specified frequency using the Series.asfreq() method. Her, in this example, we converted the TimeSeries to the 'H' (Hour). See the below example.

import pandas as pd
index = pd.date_range('1/4/2021', periods=4, freq='T')
series = pd.Series([1.0, None, None, 3.0], index=index)
print("--------The Series is-------")
print(series)
print("-----------After converting the Timeseries-----------")
print(series.asfreq(freq='H'))


--------The Series is-------
2021-01-04 00:00:00 1.0
2021-01-04 00:01:00 NaN
2021-01-04 00:02:00 NaN
2021-01-04 00:03:00 3.0
Freq: T, dtype: float64
-----------After converting the Timeseries-----------
2021-01-04 1.0
Freq: H, dtype: float64

Example: Upsampling the TimeSeries using the Series.asfreq() method

Here, in this example, we are upsampling the TimeSeries to '40s' and fill the missing values with the scalar '5.0'. See the below example.

The fill_value parameter in the Series.asfreq() method does not fill the missing values that are already present.

import pandas as pd
index = pd.date_range('1/4/2021', periods=4, freq='T')
series = pd.Series([1.0, None, None, 3.0], index=index)
print("--------The Series is-------")
print(series)
print("-----------upsample Timeseries and fill value-----------")
print(series.asfreq(freq='40s',fill_value=5.0))


--------The Series is-------
2021-01-04 00:00:00 1.0
2021-01-04 00:01:00 NaN
2021-01-04 00:02:00 NaN
2021-01-04 00:03:00 3.0
Freq: T, dtype: float64
-----------upsample Timeseries and fill value-----------
2021-01-04 00:00:00 1.0
2021-01-04 00:00:40 5.0
2021-01-04 00:01:20 5.0
2021-01-04 00:02:00 NaN
2021-01-04 00:02:40 5.0
Freq: 40S, dtype: float64

Example: Upsampling the TimeSeries using bfill method

Here, in this example, we are filling the missing values using the 'bfill' method which fills the null values backward that are present in the TimeSeries. See the below example.

import pandas as pd
index = pd.date_range('1/4/2021', periods=4, freq='T')
series = pd.Series([1.0, None, None, 3.0], index=index)
print("-----------upsample Timeseries-----------")
print(series.asfreq(freq='70s'))
print("-----------backward fill-------")
print(series.asfreq(freq='70s',method='bfill'))


-----------upsample Timeseries-----------
2021-01-04 00:00:00 1.0
2021-01-04 00:01:10 NaN
2021-01-04 00:02:20 NaN
Freq: 70S, dtype: float64
-----------backward fill-------
2021-01-04 00:00:00 1.0
2021-01-04 00:01:10 NaN
2021-01-04 00:02:20 3.0
Freq: 70S, dtype: float64

Conclusion

In this tutorial, we learned the python pandas Series.asfreq() method. We learned and understood the syntax and parameter of the Series.asfreq() method and by applying this method on DataFrame we solved examples by converting time series to the specified frequency, upsample the frequency and fill the missing values.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.