﻿ Handling Missing data before Resampling Time Series data

Handling Missing data before Resampling

Pandas Resampling and Frequency Conversion: Exercise-13 with Solution

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.

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.

﻿