Python | Панды tseries.offsets.BusinessHour.onOffset
Смещения даты - это стандартный вид приращения даты, используемый для диапазона дат в Pandas. Он работает точно так же, как relativedelta с точки зрения аргументов ключевого слова, которые мы передаем. DateOffets работает следующим образом, каждое смещение определяет набор дат, соответствующих DateOffset. Например, Bday определяет этот набор как набор дат, являющихся рабочими днями (MF).
DateOffsets могут быть созданы для перемещения дат вперед на заданное количество допустимых дат. Например, Bday (2) можно добавить к дате, чтобы переместить ее на два рабочих дня вперед. Если дата не начинается с действительной даты, сначала она перемещается на действительную дату, а затем создается смещение.
Pandas tseries.offsets.BusinessHour.onOffset() function is used to check if the passed date is on offset or not.
Syntax: pandas.tseries.offsets.BusinessHour.onOffset()
Parameter : None
Returns : boolean
Example #1: Use pandas.tseries.offsets.BusinessHour.onOffset() function to check if the passed date is on offset or not.
# importing pandas as pdimport pandas as pd # Creating Timestampts = pd.Timestamp("2019-10-10 11:15:00") # Create an offsetbh = pd.tseries.offsets.BusinessHour(n = 5) # Print the Timestampprint(ts) # Print the Offsetprint(bh) |
Выход :


Now we will add the Business hour offset to the given timestamp object to increment the datetime value. We will also check if the given date is on offset or not.
# Adding the Business hour offset to the given timestampnew_timestamp = ts + bh # Print the updated timestampprint(new_timestamp) # check if the passed date is# on offset or notresult = bh.onOffset(pd.to_datetime("2010-02-12 11:00:00")) # print the resultprint(result) |
Выход :


As we can see in the output, we have successfully created an offset and added it to the given timestamp. The function has returned True indicating that the given date is on offset.
Example #2: Use pandas.tseries.offsets.BusinessHour.onOffset() function to check if the passed date is on offset or not.
# importing pandas as pdimport pandas as pd # Creating Timestampts = pd.Timestamp("2019-10-10 11:15:00") # Create an offsetbh = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(hours = 1)) # Print the Timestampprint(ts) # Print the Offsetprint(bh) |
Выход :


Now we will add the Business hour offset to the given timestamp object to increment the datetime value. We will also check if the given date is on offset or not.
# Adding the Business hour offset to the given timestampnew_timestamp = ts + bh # Print the updated timestampprint(new_timestamp) # check if the passed date is# on offset or notresult = bh.onOffset(pd.to_datetime("2010-02-12 11:00:00")) # print the resultprint(result) |
Выход :


As we can see in the output, we have successfully created an offset and added it to the given timestamp. The function has returned True indicating that the given date is on offset.
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.