跳转至

Python | Pandas tseries.offset.business day.copy

原文:https://www.geesforgeks.org/python-pandas-ts eries-offset-business day-copy/

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

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

熊猫 **tseries.offsets.BusinessDay.copy()**函数返回给定工作日偏移对象的副本。

语法:pandas.tseries.offset.business day.copy()

参数:

返回:返回副本

示例#1: 使用pandas.tseries.offsets.BusinessDay.copy()功能创建给定偏移对象的副本。

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

输出:

现在,我们将向给定的时间戳对象添加工作日偏移量,以增加日期时间值。我们还将创建给定偏移对象的副本。

# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd

# Print the updated timestamp
print(new_timestamp)

# create a copy of the given
# offset object
bd_copy = bd.copy()

# Check if the two objects are
# same or not
print(bd_copy is bd)

输出:

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

示例 2 : 使用pandas.tseries.offsets.BusinessDay.copy()功能创建给定偏移对象的副本。

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

输出:

现在,我们将向给定的时间戳对象添加工作日偏移量,以增加日期时间值。我们还将创建给定偏移对象的副本。

# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd

# Print the updated timestamp
print(new_timestamp)

# create a copy of the given
# offset object
bd_copy = bd.copy()

# Check if the two objects are
# same or not
print(bd_copy is bd)

输出:

正如我们在输出中看到的,我们已经成功地创建了 10 个工作日& 10 个小时的偏移量,并将其添加到给定的时间戳中。我们还创建了给定偏移对象的副本。



回到顶部