Python Pandas - Return frequency applied on the given DateOffset object as a string

To return the frequency applied on a given DateOffset object as a string, use the freqstr property in Pandas. This property provides a string representation of the offset frequency.

Importing Required Libraries

First, import the necessary libraries ?

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

Creating a DateOffset Object

Create a timestamp and a DateOffset object. The DateOffset allows you to add time intervals to dates ?

# Create a timestamp
timestamp = pd.Timestamp('2021-08-30 02:30:55')
print("Original Timestamp:")
print(timestamp)

# Create DateOffset with months parameter
offset = DateOffset(months=3)
print("\nDateOffset object:")
print(offset)
Original Timestamp:
2021-08-30 02:30:55

DateOffset object:
<DateOffset: months=3>

Using freqstr Property

The freqstr property returns the frequency as a string representation ?

# Get frequency string
frequency_string = offset.freqstr
print("Frequency string:")
print(frequency_string)

# Apply offset to timestamp
updated_timestamp = timestamp + offset
print("\nUpdated Timestamp:")
print(updated_timestamp)
Frequency string:
<DateOffset: months=3>

Updated Timestamp:
2021-11-30 02:30:55

Complete Example

Here's a complete example showing different DateOffset types and their frequency strings ?

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

# Create timestamp
timestamp = pd.Timestamp('2021-08-30 02:30:55')
print("Original Timestamp:")
print(timestamp)

# Different DateOffset examples
offsets = [
    DateOffset(months=3),
    DateOffset(days=15),
    DateOffset(weeks=2),
    DateOffset(hours=6)
]

print("\nDateOffset frequency strings:")
for i, offset in enumerate(offsets, 1):
    print(f"{i}. {offset.freqstr}")
    print(f"   Applied: {timestamp + offset}")
    print()
Original Timestamp:
2021-08-30 02:30:55

DateOffset frequency strings:
1. <DateOffset: months=3>
   Applied: 2021-11-30 02:30:55

2. <DateOffset: days=15>
   Applied: 2021-09-14 02:30:55

3. <DateOffset: weeks=2>
   Applied: 2021-09-13 02:30:55

4. <DateOffset: hours=6>
   Applied: 2021-08-30 08:30:55

Conclusion

The freqstr property provides a string representation of DateOffset frequency. It's useful for debugging and understanding the time interval being applied to datetime objects.

Updated on: 2026-03-26T18:04:36+05:30

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements