Python | Панды tseries.offsets.BusinessDay.onOffset

Опубликовано: 27 Марта, 2022

Смещения даты - это стандартный вид приращения даты, используемый для диапазона дат в Pandas. Он работает точно так же, как relativedelta с точки зрения аргументов ключевого слова, которые мы передаем. DateOffsets работают следующим образом, каждое смещение задает набор дат, соответствующих DateOffset. Например, Bday определяет этот набор как набор дат, являющихся рабочими днями (MF).

DateOffsets могут быть созданы для перемещения дат вперед на заданное количество допустимых дат. Например, Bday (2) можно добавить к дате, чтобы переместить ее на два рабочих дня вперед. Если дата не начинается с действительной даты, сначала она перемещается на действительную дату, а затем создается смещение.

Pandas tseries.offsets.BusinessDay.onOffset() function returns a boolean value. It returns True if the given Business day Offset is on Offset else it return False

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

Parameter : None

Returns : boolean

Example #1: Use pandas.tseries.offsets.BusinessDay.onOffset() function to check if the given Business day offset object is on offset or not.

# importing pandas as pd
import pandas as pd
  
# Creating Timestamp
ts = pd.Timestamp("2019-10-10 07:15:11")
  
# Create an offset of 5 Business days
bd = pd.tseries.offsets.BusinessDay(n = 5)
  
# Print the Timestamp
print(ts)
  
# Print the DateOffset
print(bd)

Выход :

Now we will add the Business day offset to the given timestamp object to increment the datetime value. We will also check if the given Business day offset object is on offset or not.

# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd
  
# Print the updated timestamp
print(new_timestamp)
  
# check if the offset is 
# on offset or not
result = bd.onOffset()
  
# print the result
print(result)

Выход :

As we can see in the output, we have successfully created an offset of 5 Business days and added it to the given timestamp. The given function has returned False indicating the given offset object is not on offset.

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

# importing pandas as pd
import pandas as pd
  
# Creating 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 the Timestamp
print(ts)
  
# Print the DateOffset
print(bd)

Выход :

Now we will add the Business day offset to the given timestamp object to increment the datetime value. We will also check if the given Business day offset object is on offset or not.

# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd
  
# Print the updated timestamp
print(new_timestamp)
  
# check if the offset is 
# on offset or not
result = bd.onOffset()
  
# print the result
print(result)

Выход :

As we can see in the output, we have successfully created an offset of 10 Business days & 10 hours and added it to the given timestamp. The given function has returned False indicating the given offset object is not on offset.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.