Python | Pandas dataframe.asfreq ()

Python Methods and Functions

The Pandas function dataframe.asfreq() is used to convert TimeSeries to a specified frequency. This function additionally provides a fill method for filling / backfilling missing values. Returns the original data corresponding to the new index at the specified frequency. Resample is more appropriate if an operation such as summation is needed to represent data at a new frequency.

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

Parameters:
freq: DateOffset object, or string
method: Method to use for filling holes in reindexed Series
how: For PeriodIndex only, see PeriodIndex.asfreq
normalize: Whether to reset output index to midnight
fill_value: Value to use for missing values, applied during upsampling (note this does not fill NaNs that already were present).

Returns: converted: type of caller

Example # 1: sampling time series data from weekly frequency to daily frequency

< td class = "code">

# import pandas as pd

import pandas as pd

 
# Create a form index date_time

index_values ​​ = (pd.date_range ( '1/1 / 2000' ,

  periods = 3 , freq = ' W' ))

 
# Create a series using index_values ​​
# Note that one of the series values ​​is nan

series = (pd.Series ([ 0.0 , None , 2.0 ],

  index = index_values))

 
# Create a data frame using a series

df = pd.DataFrame ({ "Col_1" : series})

 
# Print the Dataframe
df

Now sample these weekly sample data to daily sample data. By default, newly created buckets will be set to nan. Thus, use the fill_value, parameter to fill all newly created cells with the given value.

# filtering and providing padding value = 9.0

df.asfreq (freq = 'D' , fill_value = 9.0 )

Output:

Note: this does not fill in NaNs that were already present prior to sampling.

Example # 2. Sample data with a time stamp in one minute in cells 30 s. First, create a series with 5 timestamps per minute.

# import pandas as pd

import pandas as pd

  
# Create index of the date_time form

index_values ​​ = (pd.date_range ( '1/1 / 2000' ,

periods = 5 , freq = 'T' ))

 
# Create a series using index_values ​​ # Note that one of the series values ​​is nan

series = (pd.Series ([ 0.0 , 1.0 , None , 3.0 , 4.0 ],

  index = index_values))

 
# Create a data frame using a series

df = pd.DataFrame ({ " Col_1 " : series})

  
# Print the Dataframe
df

Now sampling into 30 second bins and providing fill_value 100.0

# fetch and provide padding value 100.0

df.asfreq (freq = '30S' , fill_value = 100.0 )

Output:

Note: the Nan value existing before the selection is filled will not be





Tutorials