Pandas: Using infer_freq() function (5 examples)

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

Introduction

Pandas is a powerful library in Python widely used for data manipulation and analysis. One important aspect of time series data analysis is identifying the frequency of the data points, such as daily, monthly, or yearly. This is where the infer_freq() function comes into play, allowing users to infer the frequency of a given time series. This tutorial will guide you through the usage of infer_freq() with five practical examples, moving from simple to more complex scenarios.

Understanding infer_freq()

The infer_freq() function is part of the Pandas library, designed to deduce the frequency of a timestamp array or a DatetimeIndex. The inferred frequency can then be used for resampling, filling missing dates, and generating date ranges with the correct frequency for further analysis.

Example 1: Basic Usage

import pandas as pd
from pandas import date_range

# Create a DatetimeIndex
dates = pd.date_range('2023-01-01', periods=5, freq='D')
print('Original Frequency:', dates.freq)

# Use infer_freq to find the frequency
frequency = pd.infer_freq(dates)
print('Inferred Frequency:', frequency)

Output:

Original Frequency: D 
Inferred Frequency: D

This example demonstrates the basic usage of infer_freq(). The function successfully infers the daily frequency (‘D’) from the DatetimeIndex.

Example 2: Irregular Time Series

import pandas as pd

# Create an irregular DatetimeIndex
irregular_dates = pd.DatetimeIndex(['2023-01-01', '2023-01-03', '2023-01-06', '2023-01-10'])
print('Original Dates:', irregular_dates)

# Infer frequency
frequency = pd.infer_freq(irregular_dates)
print('Inferred Frequency:', frequency)

Output:

Original Dates: DatetimeIndex(['2023-01-01', '2023-01-03', '2023-01-06', '2023-01-10'], dtype='datetime64[ns]', freq=None) 

Inferred Frequency: None

In this case, the function returns None, indicating it couldn’t infer a specific frequency due to the irregularity of the dates.

Example 3: Filling Missing Dates and Inferring Frequency

import pandas as pd
from pandas import date_range

# Generate a date range with missing dates
dates_missing = pd.date_range('2023-01-01', periods=8, freq='2D')
# Fill missing dates
dates_filled = dates_missing.asfreq('D', 'pad')
print('Filled Frequency:', dates_filled.freq)

# Use infer_freq to re-evaluate the frequency
frequency = pd.infer_freq(dates_filled)
print('Inferred Frequency after filling:', frequency)

Output:

Filled Frequency: D 
Inferred Frequency after filling: D

This example highlights how infer_freq() can be effectively used after filling missing dates to verify the new frequency of the DatetimeIndex.

Example 4: Combining Multiple Time Series with Different Frequencies

import pandas as pd

# Combine multiple time series
series1 = pd.date_range('2023-01-01', periods=5, freq='D')
series2 = pd.date_range('2023-01-02', periods=5, freq='W')
combined_series = series1.append(series2).sort_values()

# Infer frequency
frequency = pd.infer_freq(combined_series)
print('Inferred Frequency:', frequency)

Output:

Inferred Frequency: None

This scenario shows the limitation of infer_freq() when dealing with combined time series of different frequencies, resulting in an inability to infer a clear frequency.

Example 5: Advanced – Inferring Frequency with Gaps

import pandas as pd
from pandas import date_range

# Generate a date range with intentional gaps
complex_dates = pd.date_range('2023-01-01', '2023-02-01', freq='5D')
# Create gaps
complex_dates = complex_dates.delete([2, 4])

# Infer frequency
frequency = pd.infer_freq(complex_dates)
print('Inferred Frequency with gaps:', frequency)

Output:

Inferred Frequency with gaps: None

In this example, despite the original dataset being generated with a specific frequency, the deletion of elements resulted in gaps that made it difficult for infer_freq() to infer the original frequency.

Conclusion

The infer_freq() function is a valuable tool in the Pandas library for analyzing time series data. However, its ability to accurately infer frequency depends heavily on the regularity and completeness of the data. Understanding its use cases, limitations, and combining it with other data handling techniques can greatly enhance time series analysis.