Change language

Python | Pandas tseries.offsets.BusinessDay.rollback

DateOffsets can be created to move dates by a specified number of valid dates. For example, Bday (2) can be added to a date to move it two business days forward. If the date does not start with a valid date, it is first moved to a valid date and then an offset is generated.

tseries.offsets.BusinessDay.rollback() Pandas tseries.offsets.BusinessDay.rollback() specified date back to next offset, unless offset.

Syntax : pandas.tseries.offsets.BusinessDay.rollback (dt)

Parameter:
dt: dt

Returns: Timestamp

Example # 1: Use pandas.tseries.offsets.BusinessDay.rollback ( ) to flip the supplied date back to the next offset, if not at an offset.

# import pandas as pd

import pandas as pd

 
# Create a timestamp

ts = pd.Timestamp ( ’2019-10-10 07:15 : 11’ )

 
# Create offset 5 business days

bd = pd.tseries.offsets.BusinessDay (n = 5 )

 
# Print time stamp

print (ts)

 
# Print DateOffset

print (bd)

Output:

Now we will add a desktop offset days to the given timestamp object to increment the datetime value. We’ll also roll the provided date back to the next offset, if not at the offset.

# Add a workday offset to this timestamp

new_timestamp = ts + bd

 
# Print updated timestamp

print (new_timestamp)

 
# collapse the specified date back
# if not by offset

date = bd.rollback (dt = pd .to_datetime ( ’2010-02-13’ ))

 
# print date

print (date)

Output:

As we can see from the output, we have successfully created an offset of 5 business days and added it to the given timestamp. We’ve also rolled back the provided date back to the next offset, if not by offset.

Example # 2: Use pandas.tseries.offsets.BusinessDay.rollback () to flip the supplied date back to the next offset, if not at an offset.

# import pandas as pd

import pandas as pd

 
# Create timestamp

ts = pd.Timestamp ( ’2019-10-10 07: 15: 11’ )

 
# Create an offset of 10 business days and 10 hours

bd = pd.tseries.offsets.BusinessDay (offset = datetime.timedelta (days = 10 , hours = 10 ))

 
# Print time stamp

print (ts)

 
# Print DateOffset

print (bd)

Output:

Now we will add a workday offset to the given my timestamp object to increment the datetime value. We’ll also roll the provided date back to the next offset, if not at the offset.

# Add a workday offset to this timestamp

new_timestamp = ts + bd

 
# Print updated timestamp

print (new_timestamp)

 
# roll back the provided date if not
# at offset

date = bd. rollback (dt = pd.to_datetime ( ’ 2010-02-13’ ))

 
# print date

print (date)

Output:

As we can see from the output, we have successfully created an offset of 10 business days and 10 hours and added it to the given time stamp. We’ve also rolled the provided date back to the next offset, if not the offset.

Shop

Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best laptop for development

$499+
Gifts for programmers

Best laptop for Cricut Maker

$299+
Gifts for programmers

Best laptop for hacking

$890
Gifts for programmers

Best laptop for Machine Learning

$699+
Gifts for programmers

Raspberry Pi robot kit

$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically