Python Pandas - Return the frequency applied on the given DateOffset object


To return the frequency applied on the given DateOffset object, use the offset.freqstr in Pandas. At first, import the required libraries −

from pandas.tseries.frequencies import to_offset
import pandas as pd

Set the timestamp object in Pandas −

timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

Create the DateOffset. We are incrementing the days here using the "D" frequency −

offset = to_offset("5D")

Display the Updated Timestamp −

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

Return the frequency applied on the given DateOffset object −

print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

Example

Following is the code −

from pandas.tseries.frequencies import to_offset
import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the DateOffset
# We are incrementing the days here using the "D" frequency
offset = to_offset("5D")

# Display the DateOffset
print("\nDateOffset...\n",offset)

# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)

# return the frequency applied on the given DateOffset object
print("\nThe frequency on the DateOffset object..\n", offset.freqstr)

Output

This will produce the following code −

Timestamp...
 2021-09-26 03:25:02.000045

DateOffset...
 <5 * Days>

Updated Timestamp...
 2021-10-01 03:25:02.000045

The frequency on the DateOffset object..
 5D

Updated on: 21-Oct-2021

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements