Exploring pandas.Series.asfreq() method (4 examples)

Updated: February 18, 2024 By: Guest Contributor Post a comment

Overview

The pandas.Series.asfreq() method is a powerful tool when it comes to time-series data analysis in Python. This method allows users to change the frequency of their time series data, such as converting daily data to monthly data. This tutorial explores the asfreq method through four comprehensive examples, ranging from basic to advanced applications. It is assumed that you have basic knowledge of pandas and time-series data.

The Purpose of pandas.Series.asfreq()

Before diving into the examples, let’s first understand what asfreq is and why it’s useful. Often, when working with time-series data, you’ll find that data points are either too densely packed (e.g., hourly readings) or too sparse (e.g., annual summaries) for your analysis. The asfreq method provides a simple way to resample your data to a desired frequency, filling or dropping data points as necessary.

Example 1: Basic Usage

Let’s start with a simple example. Imagine you have daily temperature readings and want to resample these readings to a monthly frequency to observe broader trends.

import pandas as pd
from pandas import Timestamp

# Creating a pandas Series with daily frequency
s = pd.Series([22, 23, 21, 25, 24, 26, 23],
              index=pd.date_range('2023-04-01', periods=7))

# Resampling to monthly frequency
monthly_s = s.asfreq('M')

# Output
print(monthly_s)

In this example, we’ve used asfreq('M') to change our daily series to monthly frequency. The resulting series only contains the data point for the end of the month.

Example 2: Filling Missing Data

When you resample data to a less frequent interval, you might end up with NaN values for periods without any data. The asfreq method offers an easy way to handle these missing values.

import pandas as pd

# Continuing with the previous series

# Resampling daily to weekly frequency with forward fill
weekly_s = s.asfreq('W', method='ffill')

# Output
print(weekly_s)

In this example, the series was resampled to a weekly frequency, and we used method='ffill' to forward-fill the missing data points.

Example 3: Adjusting Start/End of Frequency

Depending on your needs, you might want to adjust the starting or ending point of the resampled frequency. This is especially useful in financial time series, where business weeks and trading days don’t align with the calendar weeks and days.

import pandas as pd

# Sample financial time series data
fseries = pd.date_range('2023-01-01', periods=120, freq='B')
finance_s = pd.Series(range(120), index=fseries)

# Resampling to monthly frequency, adjusting to business month end
mfinance_s = finance_s.asfreq('BM')

# Output
print(mfinance_s)

By using 'BM' (business month end) instead of 'M', we ensure that the resampled data points align with the end of the business month, which is more relevant for financial analysis.

Example 4: Custom Frequency and Holidays

For more advanced scenarios, you might want to account for custom frequencies or public holidays. Pandas doesn’t directly support custom holidays in the asfreq method, but you can workaround this limitation using the CustomBusinessDay object.

from pandas.tseries.offsets import CustomBusinessDay
import pandas as pd

# Defining a custom business day to exclude weekends and a set of holidays
holidays = ['2023-07-04', '2023-12-25']
cbd = CustomBusinessDay(holidays=holidays)

# Creating a series with our custom business day frequency
s_cbd = pd.Series(pd.date_range('2023-01-01', periods=60, freq=cbd))

# Resampling the series
s_cbd_freq = s_cbd.asfreq('CBM')

# Output
print(s_cbd_freq)

This example demonstrates how to define a custom business day that accounts for weekends and public holidays and then use this to resample our time series. Note that 'CBM' (custom business month end) is not a standard pandas offset alias, and this example is illustrative of what you might try to accomplish with custom frequencies.

Conclusion

The pandas.Series.asfreq() method is a versatile tool for resampling time series data to different frequencies. Through the examples in this tutorial, you’ve seen how to adjust the frequency of your data series, fill missing data, and handle more complex scenarios like financial time series and custom business days. Understanding and utilizing this method can greatly enhance your time series data analysis, allowing for more refined and meaningful insights.