python offset函数_Python Pandas tseries.offsets.BusinessHour.copy用法及代码示例

本文介绍了Pandas中DateOffsets的使用方法,包括如何通过BusinessHour偏移量更新时间戳,并演示了如何复制偏移对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

Pandas tseries.offsets.BusinessHour.copy()函数用于创建给定偏移对象的副本。

用法: pandas.tseries.offsets.BusinessHour.copy()

参数:没有

返回:复制

范例1:采用pandas.tseries.offsets.BusinessHour.copy()函数创建给定偏移对象的副本。

# 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)

输出:

现在,我们将营业时间偏移量添加到给定的timestamp对象,以增加datetime值。我们还将创建给定偏移对象的副本。

# Adding the Business hour offset to the given timestamp

new_timestamp = ts + bh

# Print the updated timestamp

print(new_timestamp)

# create a copy

bh_copy = bh.copy()

# Check if both objects are same

print(bh is bh_copy)

输出:

从输出中可以看到,我们已经成功创建了一个偏移并将其添加到给定的时间戳中。我们还测试了两个副本是否相同。

范例2:采用pandas.tseries.offsets.BusinessHour.copy()函数创建给定偏移对象的副本。

# 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)

输出:

现在,我们将营业时间偏移量添加到给定的timestamp对象,以增加datetime值。我们还将创建给定偏移对象的副本。

# Adding the Business hour offset to the given timestamp

new_timestamp = ts + bh

# Print the updated timestamp

print(new_timestamp)

# create a copy

bh_copy = bh.copy()

# Check if both objects are same

print(bh is bh_copy)

输出:

从输出中可以看到,我们已经成功创建了一个偏移并将其添加到给定的时间戳中。我们还测试了两个副本是否相同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值