Return Frequency from DateOffset Object as String in Python Pandas



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

At first, import the required libraries −

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

Set the timestamp object in Pandas −

timestamp = pd.Timestamp('2021-08-30 02:30:55')

Create the DateOffset. We are incrementing the months here using the "months" parameter −

offset = pd.tseries.offsets.DateOffset(months=3)

Display the Updated Timestamp −

print("\nUpdated Timestamp...\n",timestamp + offset)

Frequency applied on the given DateOffset object as a string −

print("\nFrequency on the given DataOffset...\n",offset.freqstr)

Example

Following is the code −

Open Compiler
from pandas.tseries.offsets import DateOffset import pandas as pd # Set the timestamp object in Pandas timestamp = pd.Timestamp('2021-08-30 02:30:55') # Display the Timestamp print("Timestamp...\n",timestamp) # Create the DateOffset # We are incrementing the months here using the "months" parameter offset = pd.tseries.offsets.DateOffset(months=3) # Display the DateOffset print("\nDateOffset...\n",offset) # Display the Updated Timestamp print("\nUpdated Timestamp...\n",timestamp + offset) # frequency applied on the given DateOffset object as a string print("\nFrequency on the given DataOffset...\n",offset.freqstr)

Output

This will produce the following code −

Timestamp...
2021-08-30 02:30:55

DateOffset...
<DateOffset: months=3>

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

Frequency on the given DataOffset...
<DateOffset: months=3>
Updated on: 2021-10-21T06:21:46+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements