w3resource

Handling Missing data before Resampling


13. Handle Missing Data Before Resampling

Write a Pandas program to handle missing data before Resampling.

Sample Solution:

Python Code :

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

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

# Introduce some missing values
ts.iloc[10:20] = np.nan

# Fill missing values using forward fill method
ts_filled = ts.ffill()

# Resample the time series to daily frequency
ts_daily = ts_filled.resample('D').mean()

# Display the resampled time series
print(ts_daily)

Output:

2023-01-01    0.672487
2023-01-02   -0.014764
2023-01-03    0.043870
2023-01-04   -0.190643
2023-01-05   -0.639927
Freq: D, dtype: float64

Explanation:

  • Import Pandas and NumPy libraries.
  • Create a date range with hourly frequency.
  • Generate a random time series data with the created date range.
  • Introduce missing values in the time series.
  • Fill missing values using the forward fill method.
  • Resample the time series data to daily frequency by calculating the mean.
  • Print the resampled time series data.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to identify and fill missing values in a time series before performing resampling.
  • Write a Pandas program to impute missing data using forward-fill before downsampling a time series.
  • Write a Pandas program to drop rows with missing timestamps before resampling and compare the size of the original dataset.
  • Write a Pandas program to apply a custom missing data imputation strategy prior to resampling a time series dataset.

Go to:


Previous: Calculating Rolling Mean of Resampled data.
Next: Creating Custom Resampling periods.

Python Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.