Python Pandas - Check whether the DateOffset value has been normalized or not

PythonServer Side ProgrammingProgramming

To check whether the DateOff set value has been normalized or not, use the offset.normalize 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-09-26 03:25:02.000045')

Create the DateOffset. Incrementing the months here using the "months" parameter. We have normalized the DateOffset using the "normalize" parameter −

offset = pd.tseries.offsets.DateOffset(months=4, normalize=True)

Display the Updated Timestamp −

print("
Updated Timestamp...
",timestamp + offset)

Check whether the DateOffset is normalized or not −

print("
The DateOffset is normalized..
", offset.normalize)

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-09-26 03:25:02.000045')

# Display the Timestamp
print("Timestamp...
",timestamp)

# Create the DateOffset
# Incrementing the months here using the "months" parameter
# We have normalized the DateOffset using the "normalize" parameter
offset = pd.tseries.offsets.DateOffset(months=4, normalize=True)

# Display the DateOffset
print("
DateOffset...
",offset)

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

# check whether the DateOffset is normalized or not
print("
The DateOffset is normalized..
", offset.normalize)

Output

This will produce the following code −

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

DateOffset...
 <DateOffset: months=4>

Updated Timestamp...
 2022-01-26 00:00:00

The DateOffset is normalized..
 True
raja
Updated on 21-Oct-2021 07:54:57

Advertisements