Pandas DataFrame asfreq() Method with Example

DataFrame.asfreq() Method: Learn about the Pandas DataFrame asfreq() Method with its usages, syntax, and examples. By Pranit Sharma Last updated : September 30, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Python pandas.DataFrame.asfreq() Method

Pandas DataFrame.asfreq() method is used to convert Time-Series to a specific frequency. This function enables us to frame/fill back the missing values. It Returns the original data obeying a new index with the specified frequency. This method is useful when we perform operations like summarization, which can be necessary to represent the data at the new frequency.

Syntax

The syntax of DataFrame.asfreq() method is:

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

Parameter(s)

The parameters of DataFrame.asfreq() method are:

  • freq: String or object
  • method: specific method to fill the holes.
  • how: For PeriodIndex only (see PeriodIndex.asfreq).
  • normalize: option to reset the index or not
  • fill_value: values to be replaced with.

Let us understand with the help of an example,

Pandas DataFrame asfreq() Method Example

# Importing pandas package
import pandas as pd

# Creating dictionary
d = {'col':[1.0,2.0,3.0]}

# Creating DataFrame
df = pd.DataFrame(d,index=pd.date_range('16/09/2022',
                   periods=3,freq='W'))

# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")

# Using asfreq method
df = df.asfreq(freq ='D', fill_value = 15.0)

# Display modified DataFrame
print("Modified DataFrame:\n",df)

Output

The output of the above program is:

Example: Pandas DataFrame asfreq() Method

Reference: pandas.DataFrame.asfreq()

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.