w3resource

Resampling Time Series data to Business day Frequency

Pandas Resampling and Frequency Conversion: Exercise-6 with Solution

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.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://198.211.115.131/python-exercises/pandas/resampling-frequency-conversion/resampling-time-series-data-to-business-day-frequency.php