w3resource

Interpolating Missing values after Resampling

Pandas Resampling and Frequency Conversion: Exercise-7 with Solution

Write a Pandas program to interpolate missing values after Resampling.

Sample Solution:

Python Code :

# Import necessary libraries
import pandas as pd
import numpy as np

# Create a time series data with daily frequency
date_rng = pd.date_range(start='2020-01-01', end='2020-01-05', freq='D')
ts = pd.Series(np.random.randn(len(date_rng)), index=date_rng)

# Introduce some missing values
ts.iloc[2] = np.nan

# Resample the time series to hourly frequency
ts_hourly = ts.resample('H').ffill()

# Interpolate missing values
ts_interpolated = ts_hourly.interpolate()

# Display the interpolated time series
print(ts_interpolated)

Output:

2020-01-01 00:00:00    1.172812
2020-01-01 01:00:00    1.172812
2020-01-01 02:00:00    1.172812
2020-01-01 03:00:00    1.172812
2020-01-01 04:00:00    1.172812
  
2020-01-04 20:00:00   -1.386435
2020-01-04 21:00:00   -1.386435
2020-01-04 22:00:00   -1.386435
2020-01-04 23:00:00   -1.386435
2020-01-05 00:00:00   -0.720658
Freq: H, Length: 97, dtype: float64

Explanation:

  • Import Pandas and NumPy libraries.
  • Create a date range with daily frequency.
  • Generate a random time series data with the created date range.
  • Introduce missing values in the time series.
  • Resample the time series data to hourly frequency by forward filling the values.
  • Interpolate the missing values in the resampled time series.
  • Print the interpolated time series data.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Resampling Time Series data to Business day Frequency.
Next: Resampling Time Series data with Custom functions.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



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://w3resource.com/python-exercises/pandas/resampling-frequency-conversion/interpolating-missing-values-after-resampling-time-series.php