Resampling Time Series data to daily Frequency
Pandas Resampling and Frequency Conversion: Exercise-1 with Solution
Write a Pandas program to resample time series data to daily frequency.
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)
# Resample the time series to daily frequency
ts_daily = ts.resample('D').mean()
# Display the resampled time series
print(ts_daily)
Output:
2023-01-01 0.079969 2023-01-02 -0.174736 2023-01-03 0.052439 2023-01-04 0.097063 2023-01-05 0.437323 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.
- 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.
Previous:Pandas Resampling Frequency Conversion Exercises Home.
Next: Upsampling Time Series data from daily to Hourly Frequency.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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/resampling-time-series-data-to-daily-frequency-with-pandas.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics