w3resource

Resampling Time Series data to daily Frequency


1. Resample Time Series Data to Daily Frequency

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.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to resample a minute-level time series to daily frequency using the mean and verify the date index format.
  • Write a Pandas program to convert an irregular time series to a regular daily frequency, filling missing dates with NaN.
  • Write a Pandas program to resample a time series to daily frequency and forward-fill missing values after aggregation.
  • Write a Pandas program to resample a time series dataset to daily frequency and then compute the daily range (max-min) for each day.

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.



Follow us on Facebook and Twitter for latest update.