Change language

Python | Pandas tseries.offsets.BusinessDay.onOffset

| |

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.onOffset() Pandas tseries.offsets.BusinessDay.onOffset() returns a boolean value. Returns True if the given workday offset is set to Offset, otherwise it returns False

Syntax: pandas. tseries.offsets.BusinessDay.onOffset ()

Parameter: None

Returns: boolean

Example # 1: Use pandas.tseries.offsets.BusinessDay.onOffset () to check if a given offset object is pandas.tseries.offsets .BusinessDay.onOffset () day in offset or not.

# import pandas as pd

import pandas as pd

 
# Create timestamp

ts = p d.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 the workday offset to this timestamp object to increase the datetime value. We will also check if the given workday offset object is at the offset or not.

# Add a workday offset to this timestamp

new_timestamp = ts + bd

 
# Print updated timestamp

print (new_timestamp)

 
# check if offset
# by offset or not

result = bd. onOffset ()

 
  # print the result

print (result)

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. This function returned False indicating that the given offset object is not in offset.

Example # 2: Use pandas.tseries.offsets .BusinessDay.onOffset () to check if the given offset object is pandas.tseries.offsets.BusinessDay.onOffset () day at offset or not.

# import pandas as pd

import pandas as pd

 
# Create a timestamp

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

  
# Create offset 10 working days it 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 the workday offset to this timestamp object to increase the datetime value. We will also check if the given workday offset object is at the offset or not.

# Add a workday offset to this time stamp

new_timestamp = ts + bd

 
# Print updated timestamp

print (new_timestamp)

 
# check if offset
# by offset or not

result = bd. onOffset ()

 
  # print the result

print (result)

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. This function returned False indicating that the given offset object is not in offset.