﻿ Resampling Time Series data to Quarterly Frequency with Pandas

# Resampling Time Series data to Quarterly Frequency

## Pandas Resampling and Frequency Conversion: Exercise-9 with Solution

Write a Pandas program to resample Time Series data to quarterly frequency.

Sample Solution:

Python Code :

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

# Create a time series data with monthly frequency
date_rng = pd.date_range(start='2019-01-01', end='2019-12-01', freq='M')
ts = pd.Series(np.random.randn(len(date_rng)), index=date_rng)

# Resample the time series to quarterly frequency
ts_quarterly = ts.resample('Q').mean()

# Display the resampled time series
print(ts_quarterly)
``````

Output:

```2019-03-31    0.045935
2019-06-30   -1.032537
2019-09-30    0.062271
2019-12-31    1.227764
Freq: Q-DEC, dtype: float64
```

Explanation:

• Import Pandas and NumPy libraries.
• Create a date range with monthly frequency.
• Generate a random time series data with the created date range.
• Resample the time series data to quarterly 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.

﻿