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

PythonServer Side ProgrammingProgramming

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 −

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>
raja
Updated on 21-Oct-2021 06:21:46

Advertisements