pandas.DataFrame.asfreq() Function | Convert time series in panda

Spread the love

pandas.DataFrame.asfreq() function : This function convert TimeSeries to specified frequency. Optionally provide filling method to pad/backfill missing values. 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 : {‘backfill’/’bfill’, ‘pad’/’ffill’}, default None
Method to use for filling holes in reindexed Series (note this does not fill NaNs that already were present):

‘pad’ / ‘ffill’: propagate last valid observation forward to next valid
‘backfill’ / ‘bfill’: use NEXT valid observation to fill
how : {‘start’, ‘end’}, default end
For PeriodIndex only, see PeriodIndex.asfreq

normalize : bool, default False
Whether to reset output index to midnight

fill_value : scalar, optional
Value to use for missing values, applied during upsampling (note this does not fill NaNs that already were present).

converted : same type as caller

import pandas as pd index = pd.date_range('31/3/2018', periods=4, freq='T') series = pd.Series([0.0, 1.0, 2.0, 3.0], index=index) df = pd.DataFrame({'data':series}) print(df.asfreq(freq='45S')) print(df.asfreq(freq='1H'))


2018-03-31 00:00:00   0.0
2018-03-31 00:00:45   NaN
2018-03-31 00:01:30   NaN
2018-03-31 00:02:15   NaN
2018-03-31 00:03:00   3.0
2018-03-31   0.0



Leave a Reply

Your email address will not be published. Required fields are marked *