Python | Pandas dataframe.asfreq()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
dataframe.asfreq() function is used to convert TimeSeries to specified frequency. This function Optionally provide filling method to pad/backfill missing values. It Returns the original data conformed to a new index with the specified frequency. resample is more appropriate if an operation, such as summarization, is necessary to represent the data at the new frequency.
Syntax : DataFrame.asfreq(freq, method=None, how=None, normalize=False, fill_value=None)
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: Unsample a time series data from weekly frequency to daily frequency
Now unsample this weekly sampled data into daily sampled data.By default newly created bins will have nan value. So, use fill_value parameter to fill all newly created bins with a provided value.
Note : This does not fill NaNs that already were present before sampling.
Example #2: Unsample a one minute timestamped data into 30s bins.First create a series with 5 one minute timestamps.
Now Unsampling into 30-second bins and providing a fill_value of 100.0
Note : Nan value present before unsampling will not be filled