import pandas as pd # define time series data ts = pd.Series([10, 20, 30, 40], index=pd.date_range('20210101', periods=4, freq='D')) # convert frequency to weekly ts_weekly = ts.asfreq('W') print(ts) print(ts_weekly)
2021-01-01 10 2021-01-02 20 2021-01-03 30 2021-01-04 40 Freq: D, dtype: int64 2021-01-03 30.0 2021-01-10 NaN 2021-01-17 NaN 2021-01-24 NaN Freq: W-SUN, dtype: float64
import pandas as pd # define time series data ts = pd.Series([10, 20, 30, 40], index=pd.date_range('20210101', periods=4, freq='D')) # convert frequency to hourly and fill missing values with linear interpolation ts_hourly = ts.asfreq('H', method='linear') print(ts) print(ts_hourly)
2021-01-01 10 2021-01-02 20 2021-01-03 30 2021-01-04 40 Freq: D, dtype: int64 2021-01-01 00:00:00 10.0 2021-01-01 01:00:00 11.0 2021-01-01 02:00:00 12.0 2021-01-01 03:00:00 13.0 2021-01-01 04:00:00 14.0 2021-01-01 05:00:00 15.0 2021-01-01 06:00:00 16.0 2021-01-01 07:00:00 17.0 2021-01-01 08:00:00 18.0 2021-01-01 09:00:00 19.0 2021-01-01 10:00:00 20.0 2021-01-01 11:00:00 21.0 2021-01-01 12:00:00 22.0 2021-01-01 13:00:00 23.0 2021-01-01 14:00:00 24.0 2021-01-01 15:00:00 25.0 2021-01-01 16:00:00 26.0 2021-01-01 17:00:00 27.0 2021-01-01 18:00:00 28.0 2021-01-01 19:00:00 29.0 2021-01-01 20:00:00 30.0 2021-01-01 21:00:00 31.0 2021-01-01 22:00:00 32.0 2021-01-01 23:00:00 33.0 2021-01-02 00:00:00 34.0 2021-01-02 01:00:00 35.0 2021-01-02 02:00:00 36.0 2021-01-02 03:00:00 37.0 2021-01-02 04:00:00 38.0 2021-01-02 05:00:00 39.0 2021-01-02 06:00:00 40.0 Freq: H, dtype: float64In this example, we convert the same time series to an hourly frequency and fill in the missing values using linear interpolation. The resulting time series has hourly data points and values that have been interpolated from the original data.