w3resource

Resampling Time Series data to Business day Frequency


6. Resample Time Series Data to Business Day Frequency

Write a Pandas program to resample Time Series Data to Business day Frequency.

Sample Solution:

Python Code :

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

# Create a time series data with daily frequency including weekends
date_rng = pd.date_range(start='2020-01-01', end='2020-01-10', freq='D')
ts = pd.Series(np.random.randn(len(date_rng)), index=date_rng)

# Resample the time series to business day frequency
ts_bday = ts.resample('B').mean()

# Display the resampled time series
print(ts_bday) 

Output:

2020-01-01    0.464848
2020-01-02    0.110731
2020-01-03   -0.322476
2020-01-06    1.465602
2020-01-07   -0.410054
2020-01-08   -1.832310
2020-01-09    1.634878
2020-01-10   -0.236743
Freq: B, dtype: float64

Explanation:

  • Import Pandas and NumPy libraries.
  • Create a date range with daily frequency including weekends.
  • Generate a random time series data with the created date range.
  • Resample the time series data to business day 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 daily time series to business day frequency and fill non-business days with NaN.
  • Write a Pandas program to resample a time series to business day frequency, then forward-fill missing business days.
  • Write a Pandas program to adjust a calendar of events to business day frequency, excluding weekends and holidays.
  • Write a Pandas program to resample a time series to business day frequency and compute a rolling average over business days only.

Go to:


Previous: Downsampling Time Series Data from Minute to Hourly Frequency.
Next:Interpolating Missing values after Resampling.

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.