w3resource

Pandas Series: asfreq() function

Convert Pandas TimeSeries to specified frequency

The asfreq() function is used to 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:

Series.asfreq(self, freq, method=None, how=None, normalize=False, fill_value=None)
Pandas Series asfreq image

Parameters:

Name Description Type/Default Value Required / Optional
freq DateOffset object, or string Required
method 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
{'backfill'/'bfill', 'pad'/'ffill'}
Default Value: None
Required
how For PeriodIndex only, see PeriodIndex.asfreq {'start', 'end'}
Default Value: end
Required
normalize Whether to reset output index to midnight bool
Default Value: False
Required
fill_value Value to use for missing values, applied during upsampling (note this does not fill NaNs that already were present). scalar Optional

Returns: converted - same type as caller

Example - Start by creating a series with 4 one minute timestamps:

Python-Pandas Code:

import numpy as np
import pandas as pd
index = pd.date_range('1/1/2019', periods=4, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0], index=index)
df = pd.DataFrame({'s':series})
df

Output:

                        s
2019-01-01 00:00:00	0.0
2019-01-01 00:01:00	NaN
2019-01-01 00:02:00	2.0
2019-01-01 00:03:00	3.0
Pandas Series asfreq image

Example - Upsample the series into 20 second bins:

Python-Pandas Code:

import numpy as np
import pandas as pd
index = pd.date_range('1/1/2019', periods=4, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0], index=index)
df = pd.DataFrame({'s':series})
df.asfreq(freq='20S')

Output:

                        s
2019-01-01 00:00:00	0.0
2019-01-01 00:00:20	NaN
2019-01-01 00:00:40	NaN
2019-01-01 00:01:00	NaN
2019-01-01 00:01:20	NaN
2019-01-01 00:01:40	NaN
2019-01-01 00:02:00	2.0
2019-01-01 00:02:20	NaN
2019-01-01 00:02:40	NaN
2019-01-01 00:03:00	3.0

Example - Upsample again, providing a fill value:

Python-Pandas Code:

import numpy as np
import pandas as pd
index = pd.date_range('1/1/2019', periods=4, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0], index=index)
df = pd.DataFrame({'s':series})
df.asfreq(freq='20S', fill_value=7.0)

Output:

                        s
2019-01-01 00:00:00	0.0
2019-01-01 00:00:20	7.0
2019-01-01 00:00:40	7.0
2019-01-01 00:01:00	NaN
2019-01-01 00:01:20	7.0
2019-01-01 00:01:40	7.0
2019-01-01 00:02:00	2.0
2019-01-01 00:02:20	7.0
2019-01-01 00:02:40	7.0
2019-01-01 00:03:00	3.0

Example - Upsample again, providing a method:

Python-Pandas Code:

import numpy as np
import pandas as pd
index = pd.date_range('1/1/2019', periods=4, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0], index=index)
df = pd.DataFrame({'s':series})
df.asfreq(freq='20S', method='bfill')

Output:

                        s
2019-01-01 00:00:00	0.0
2019-01-01 00:00:20	NaN
2019-01-01 00:00:40	NaN
2019-01-01 00:01:00	NaN
2019-01-01 00:01:20	2.0
2019-01-01 00:01:40	2.0
2019-01-01 00:02:00	2.0
2019-01-01 00:02:20	3.0
2019-01-01 00:02:40	3.0
2019-01-01 00:03:00	3.0

Previous: Modify Pandas series in place using non-NA values
Next: Get the last row(s) without any NaNs in Pandas series



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/pandas/series/series-asfreq.php