Python Pandas tseries.offsets.BusinessHour

Python Pandas tseries.offsets.BusinessHour

Dateoffsets是一种标准的日期增量,用于Pandas中的日期范围。就我们传入的关键字args而言,它的工作方式与relativedelta完全一样。DateOffets的工作原理如下,每个偏移量指定一组符合DateOffset的日期。例如,Bday定义这个集合为工作日(M-F)的日期集合。

DateOffsets可以被创建,以将日期向前移动一个给定的有效日期的数量。例如,Bday(2)可以被添加到一个日期,使其向前移动两个工作日。如果该日期不是从一个有效的日期开始,首先它被移到一个有效的日期,然后创建偏移。

Pandas tseries.offsets.BusinessHour()函数被用来创建营业时间的偏移。

语法: pandas.tseries.offsets.BusinessHour(n=1, normalize=False, start='09:00′, end='17:00′, offset=datetime.timedelta(0) )

参数:
n :小时数
normalize : 是否将DateOffset加法的结果向下舍入到前一个午夜。
start:开始时间
end:结束时间
offset :从给定日期开始的偏移量

返回 : 抵偿

例子#1:使用pandas.tseries.offsets.BusinessHour()函数来创建一个5个营业时间的偏移。

# 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 hours
bh = pd.tseries.offsets.BusinessHour(n = 5)
  
# Print the Timestamp
print(ts)
  
# Print the Offset
print(bh)
Python

输出 :

Python Pandas tseries.offsets.BusinessHour

Python Pandas tseries.offsets.BusinessHour

现在,我们将在给定的时间戳对象上添加营业时间的偏移量,以增加数据时间的值。

# Adding the Business hour offset to the given timestamp
new_timestamp = ts + bh
  
# Print the updated timestamp
print(new_timestamp)
Python

输出 :

Python Pandas tseries.offsets.BusinessHour

正如我们在输出中所看到的,我们已经成功地创建了一个5个商业小时的偏移量,并将其添加到给定的时间戳中。

例子#2:使用pandas.tseries.offsets.BusinessHour()函数来创建一个10天和5个工作小时的偏移。

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

输出 :

Python Pandas tseries.offsets.BusinessHour

Python Pandas tseries.offsets.BusinessHour

现在,我们将在给定的时间戳对象上添加营业时间的偏移量,以增加数据时间的值。

# Adding the Business hour offset to the given timestamp
new_timestamp = ts + bh
  
# Print the updated timestamp
print(new_timestamp)
Python

输出 :

Python Pandas tseries.offsets.BusinessHour

正如我们在输出中看到的,我们已经成功地创建了一个偏移量,并将其添加到给定的时间戳中。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Pandas 日期时间

登录

注册