Pandas Series Asfreq Method

PythonPythonBeginner
Practice Now

Introduction

The Series.asfreq() method in Pandas is used to convert a time series to a specified frequency. It allows us to fill in missing values or null values in the time series. This tutorial will guide you on how to use the Series.asfreq() method in Python Pandas.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/AdvancedOperationsGroup(["`Advanced Operations`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/AdvancedOperationsGroup -.-> pandas/time_series_analysis("`Time Series Analysis`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/time_series_analysis -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} python/lists -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} python/tuples -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} python/importing_modules -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} python/numerical_computing -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} python/data_analysis -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} python/build_in_functions -.-> lab-68742{{"`Pandas Series Asfreq Method`"}} end

Import the necessary libraries

import pandas as pd

Create a datetime index

index = pd.date_range('1/4/2021', periods=4, freq='T')

Create a Series with missing values

series = pd.Series([1.0, None, None, 3.0], index=index)

Print the original Series

print("--------The Series is-------")
print(series)

Convert the Series to a specified frequency

print("-----------After converting the Timeseries-----------")
print(series.asfreq(freq='H'))

Upsample the Series and fill missing values

print("-----------Upsample Timeseries and fill values-----------")
print(series.asfreq(freq='40s', fill_value=5.0))

Upsample the Series using 'bfill' method

print("-----------Upsample Timeseries using 'bfill' method-----------")
print(series.asfreq(freq='70s', method='bfill'))

Summary

In this tutorial, we learned how to use the Series.asfreq() method in Python Pandas. We used this method to convert a time series to a specified frequency and filled in missing values or null values. We also learned how to upsample the frequency of the time series and fill in missing values using the 'bfill' method. The Series.asfreq() method is a useful tool for working with time series data in Pandas.

Other Python Tutorials you may like