Signup/Sign In
PUBLISHED ON: MARCH 12, 2021

Pandas DataFrame asfreq() Method

In this tutorial, we will learn the python pandas DataFrame.asfreq() method. It converts TimeSeries to a specified frequency. It provides a filling method to pad or backfill the missing values. After applying this method to the DataFrame, it returns the object converted to the specified frequency. In this tutorial, we will convert time series to the specified frequency, upsample the frequency, and fill in the missing values.

Syntax

The syntax required to use this method is as follows

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

Parameters

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

method: It includes the ‘backfill’/’bfill’, ‘pad’/’ffill’, and the default value is None. Method to use for filling holes in reindexed Series (note this does not fill NaNs that already were present):

‘pad’ or ‘ffill’: propagate last valid observation forward to next valid

‘backfill’ or ‘bfill’: use NEXT valid observation to fill.

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

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

fill_value: It represents a scalar(optional) that is value to use for the missing values applied during upsampling.

Example 1: Convert Timeseries to the specified frequency

We can convert the time series to the different specified frequencies using the DataFrame.asfreq() method.

import pandas as pd
index = pd.date_range('1/1/2021', periods=4, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0], index=index)
df = pd.DataFrame({'Time':series})
print(df)
print("converting to different frequency")
print(df.asfreq(freq='H'))

Once we run the program we will get the following result.


Time
2021-01-01 00:00:00 0.0
2021-01-01 00:01:00 NaN
2021-01-01 00:02:00 2.0
2021-01-01 00:03:00 3.0
converting to different frequency
Time
2021-01-01 0.0

Example 2: Upsample Timeseries using the DataFrame.asfreq() Method

The below example shows that we can upsample the frequency of time series.

import pandas as pd
index = pd.date_range('1/1/2021', periods=4, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0], index=index)
df = pd.DataFrame({'Time':series})
print(df)
print("-----------upsample Timeseries-----------")
print(df.asfreq(freq='50s'))

Once we run the program we will get the following result.


Time
2021-01-01 00:00:00 0.0
2021-01-01 00:01:00 NaN
2021-01-01 00:02:00 2.0
2021-01-01 00:03:00 3.0
-----------upsample Timeseries-----------
Time
2021-01-01 00:00:00 0.0
2021-01-01 00:00:50 NaN
2021-01-01 00:01:40 NaN
2021-01-01 00:02:30 NaN

Example 3: Upsample Timeseries and fill missing values using the DataFrame.asfreq() Method

This example is similar to the previous example, in this example, we upsample the time series by '50s' and fill the missing values.

import pandas as pd
index = pd.date_range('1/1/2021', periods=4, freq='T')
series = pd.Series([0.0, None, None, 3.0], index=index)
df = pd.DataFrame({'Time':series})
print(df)
print("-----------upsample Timeseries and fill value-----------")
print(df.asfreq(freq='50s',fill_value=9.0))

Once we run the program we will get the following result.


Time
2021-01-01 00:00:00 0.0
2021-01-01 00:01:00 NaN
2021-01-01 00:02:00 NaN
2021-01-01 00:03:00 3.0
-----------upsample Timeseries and fill value------------
Time
2021-01-01 00:00:00 0.0
2021-01-01 00:00:50 9.0
2021-01-01 00:01:40 9.0
2021-01-01 00:02:30 9.0

Example 4: Upsample Timeseries and back fill missing values using the DataFrame.asfreq() Method

In this example, the DataFrame.asfreq() method upsample the time series and back fill the missing values.

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

Once we run the program we will get the following result.


-----------upsample Timeseries-----------
Time
2021-01-01 00:00:00 0.0
2021-01-01 00:01:10 NaN
2021-01-01 00:02:20 NaN
-----------backward fill-------
Time
2021-01-01 00:00:00 0.0
2021-01-01 00:01:10 NaN
2021-01-01 00:02:20 3.0

Conclusion

In this tutorial, we learned the python pandas DataFrame.asfreq() method. We learned and understood the syntax and parameter of the DataFrame.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.