How do we upsample a time-series using asfreq() method?

PandasServer Side ProgrammingProgramming

By using the pandas asfreq() method we can upsample a time series and also we can able to fill the Nan values using the fill_value parameter.

The pandas.Series.asfreq() method is used to convert the Time Series to the specified frequency. As a result, It will return a reindexed time series with a specified frequency.

Let's create a timeseries object by using the pandas date_range module and upsample it by using the pandas.series.asfreq() method.

Example 1

import pandas as pd

# creating dates
date = pd.date_range("2021-07-01", periods=2, freq="M")

# creating pandas Series with date range index
s = pd.Series([5, 6], index=date)
print(s)

# upsample the Timeseries with fill value
print("Output of asfreq:",s.asfreq(freq='D',fill_value=5))

Explanation

In this following example, we have created a pandas timeseries object by using the pandas date_range function. Then we are upsampled the TimeSeries to “D” days frequency from months and filled the missing values with a scalar '5'.

Output

2021-07-31 5
2021-08-31 6
Freq: M, dtype: int64

Output of asfreq:
2021-07-31 5
2021-08-01 5
2021-08-02 5
2021-08-03 5
2021-08-04 5
2021-08-05 5
2021-08-06 5
2021-08-07 5
2021-08-08 5
2021-08-09 5
2021-08-10 5
2021-08-11 5
2021-08-12 5
2021-08-13 5
2021-08-14 5
2021-08-15 5
2021-08-16 5
2021-08-17 5
2021-08-18 5
2021-08-19 5
2021-08-20 5
2021-08-21 5
2021-08-22 5
2021-08-23 5
2021-08-24 5
2021-08-25 5
2021-08-26 5
2021-08-27 5
2021-08-28 5
2021-08-29 5
2021-08-30 5
2021-08-31 6
Freq: D, dtype: int64

The initial time series is created by 2 periods with the frequency of months. After that, we upsampled that timeseries object to “D” Day frequency by using the asfreq() method. The resultant times series having 32 periods with scaler value “5”.

Example 2

import pandas as pd

# create the index
index = pd.date_range('2021-07-1', periods=5, freq='T')

#creating pandas Series with date index
series = pd.Series([2,3,None,4,5], index=index)

print(series)

# upsample the Timeseries with fill value
print("Converted time series",series.asfreq(freq='30s',fill_value=1))

Explanation

In this above example, we have created a pandas timeseries using the Date_range method, and it has 5 periods of time “T” frequency. After that, we have changed the frequency of the series object to “30s” by using the asfreq() method this process is called upsampling.

Output

2021-07-01 00:00:00 2.0
2021-07-01 00:01:00 3.0
2021-07-01 00:02:00 NaN
2021-07-01 00:03:00 4.0
2021-07-01 00:04:00 5.0
Freq: T, dtype: float64

Converted time series
2021-07-01 00:00:00 2.0
2021-07-01 00:00:30 1.0
2021-07-01 00:01:00 3.0
2021-07-01 00:01:30 1.0
2021-07-01 00:02:00 NaN
2021-07-01 00:02:30 1.0
2021-07-01 00:03:00 4.0
2021-07-01 00:03:30 1.0
2021-07-01 00:04:00 5.0
Freq: 30S, dtype: float64

In the above output block, we can see both the initial timeseries object and upsampled timeseries object. The upsampled time series object has 9 periods with a “30s” frequency.

The fill_value parameter in the pandas.Series.asfreq() method does not fill the Nan or missing values that are already present in the given timeseries object. That is the reason there is a Nan value present in the upsampled series object.

raja
Updated on 09-Mar-2022 07:17:50

Advertisements