Open In App
Related Articles

Python | Pandas tseries.offsets.DateOffset

Improve Article
Improve
Save Article
Save
Like Article
Like

Dateoffsets are a standard kind of date increment used for a date range in Pandas. It works exactly like relative delta in terms of the keyword args we pass in. DateOffsets work as follows, each offset specifies a set of dates that conform to the DateOffset. For example, Bday defines this set to be the set of dates that are weekdays (M-F). DateOffsets can be created to move dates forward a given number of valid dates. For example, Bday(2) can be added to the date to move it two business days forward. If the date does not start on a valid date, first it is moved to a valid date and then offset is created.

Pandas tseries.offsets.DateOffset is used to create a standard kind of date increment used for a date range.

Syntax:
pandas.tseries.offsets.DateOffset(n=1, normalize=False, **kwds)

Parameters:
n : The number of time periods the offset represents.
normalize : Whether to round the result of a DateOffset addition down to the previous midnight.
level : int, str, default None
**kwds : Temporal parameter that adds to or replaces the offset value. Parameters that add to the offset (like Timedelta): years, months etc.

Returns : DateOffsets

Example #1: Use pandas.tseries.offsets.DateOffset function to create dateoffsets of 2 days. 

Python3




# importing pandas as pd
import pandas as pd
 
# Creating Timestamp
ts = pd.Timestamp('2019-10-10 07:15:11')
 
# Create the DateOffset
do = pd.tseries.offsets.DateOffset(n = 2)
 
# Print the Timestamp
print(ts)
 
# Print the DateOffset
print(do)


Output :

2019-10-10 07:15:11
<2 * DateOffsets>

Now we will add the dateoffset to the given timestamp object to create an offset of 2 days from the given date. 

Python3




# Adding the dateoffset to the given timestamp
new_timestamp = ts + do
 
# Print the updated timestamp
print(new_timestamp)


Output :

2019-10-12 07:15:11

As we can see in the output, we have successfully created an offset of 2 days and added it to the given timestamp object to move the date forward by 2 days.

Example #2: Use pandas.tseries.offsets.DateOffset function to create dateoffsets of 10 days and 2 hours. 

Python3




# importing pandas as pd
import pandas as pd
 
# Creating Timestamp
ts = pd.Timestamp('2019-10-10 07:15:11')
 
# Create the DateOffset
do = pd.tseries.offsets.DateOffset(days = 10, hours = 2)
 
# Print the Timestamp
print(ts)
 
# Print the DateOffset
print(do)


Output :

2019-10-10 07:15:11
<DateOffset: days=10, hours=2>

Now we will add the dateoffset to the given timestamp object to create an offset of 10 days and 2 hours from the given date. 

Python3




# Adding the dateoffset to the given timestamp
new_timestamp = ts + do
 
# Print the updated timestamp
print(new_timestamp)


Output :

2019-10-20 09:15:11

As we can see in the output, we have successfully created an offset of 10 days and 2 hours and added it to the given timestamp object to move the date forward by 10 days and 2 hours.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials