Python | Панды tseries.offsets.BusinessHour.rule_code

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

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

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

Pandas tseries.offsets.BusinessHour.rule_code attribute return the rule_code of the frequency applied on the given offset as a string.

Syntax: pandas.tseries.offsets.BusinessHour.rule_code

Parameter : None

Returns : rule_code

Example #1: Use pandas.tseries.offsets.BusinessHour.rule_code attribute to return the rule_code of the frequency applied on the given offset as string.

# importing pandas as pd
import pandas as pd
  
# Creating Timestamp
ts = pd.Timestamp("2019-10-10 11:15:00")
  
# Create an offset
bh = pd.tseries.offsets.BusinessHour(n = 5)
  
# Print the Timestamp
print(ts)
  
# Print the Offset
print(bh)

Выход :

Now we will add the Business hour offset to the given timestamp object to increment the datetime value. We will also print the rule_code of the frequency applied on the given offset as string.

# Adding the Business hour offset to the given timestamp
new_timestamp = ts + bh
  
# Print the updated timestamp
print(new_timestamp)
  
# print the rule_code of the frequency
# applied as string
print(bh.name)

Выход :

Как видно из выходных данных, мы успешно создали смещение и добавили его к заданной временной метке. мы также напечатали rule_code частоты, применяемой к данному смещению, в виде строки.

Example #2: Use pandas.tseries.offsets.BusinessHour.rule_code attribute to return the rule_code of the frequency applied on the given offset as string.

# importing pandas as pd
import pandas as pd
  
# Creating Timestamp
ts = pd.Timestamp("2019-10-10 11:15:00")
  
# Create an offset
bh = pd.tseries.offsets.BusinessHour(offset = datetime.timedelta(hours = 1))
  
# Print the Timestamp
print(ts)
  
# Print the Offset
print(bh)

Выход :

Now we will add the Business hour offset to the given timestamp object to increment the datetime value. We will also print the rule_code of the frequency applied on the given offset as string.

# Adding the Business hour offset to the given timestamp
new_timestamp = ts + bh
  
# Print the updated timestamp
print(new_timestamp)
  
# print the rule_code of the frequency
# applied as string
print(bh.rule_code)

Выход :

Как видно из выходных данных, мы успешно создали смещение и добавили его к заданной временной метке. мы также напечатали rule_code частоты, применяемой к данному смещению, в виде строки.

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

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