Dateoffsets是用於Pandas中日期範圍的標準日期增量。就我們傳入的關鍵字args而言,它的工作方式與relativedelta完全相同。DateOffets的工作方式如下,每個偏移量指定一組符合DateOffset的日期。例如,“ Bday”將此集合定義為工作日的日期集合(M-F)。
可以創建DateOffsets以將日期向前移動給定數量的有效日期。例如,可以將Bday(2)添加到日期以將其向前移動兩個工作日。如果日期不是從有效日期開始,則首先將其移動到有效日期,然後創建偏移量。
Pandas tseries.offsets.BusinessDay.rollforward()
函數僅在不使用偏移量時才將提供的日期前滾到下一個偏移量。
用法: pandas.tseries.offsets.BusinessDay.rollforward(dt)
參數:
dt:dt
返回:時間戳記
範例1:采用pandas.tseries.offsets.BusinessDay.rollforward()
函數將提供的日期向前滾動到下一個偏移量(如果不在偏移量上)。
# 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)
輸出:
現在,我們將營業日偏移量添加到給定的timestamp對象,以增加datetime值。如果不在偏移上,我們還將提供的日期向前滾動到下一個偏移。
# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd
# Print the updated timestamp
print(new_timestamp)
# roll the provided date forward if not
# on the offset
date = bd.rollforward(dt = pd.to_datetime('2010-02-13'))
# print the date
print(date)
輸出:
從輸出中可以看到,我們已經成功創建了5個工作日的偏移並將其添加到給定的時間戳中。如果不在偏移量上,我們還將提供的日期前滾到下一個偏移量。
範例2:采用pandas.tseries.offsets.BusinessDay.rollforward()
函數將提供的日期向前滾動到下一個偏移量(如果不在偏移量上)。
# 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)
輸出:
現在,我們將營業日偏移量添加到給定的timestamp對象,以增加datetime值。如果不在偏移上,我們還將提供的日期向前滾動到下一個偏移。
# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd
# Print the updated timestamp
print(new_timestamp)
# roll the provided date forward if not
# on the offset
date = bd.rollforward(dt = pd.to_datetime('2010-02-13'))
# print the date
print(date)
輸出:
從輸出中可以看到,我們已經成功創建了10個工作日和10個小時的偏移量,並將其添加到給定的時間戳記中。如果不在偏移量上,我們還將提供的日期前滾到下一個偏移量。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Timestamp.tz用法及代碼示例
- Python Pandas Series.str.contains()用法及代碼示例
- Python Pandas dataframe.std()用法及代碼示例
- Python Pandas Timestamp.dst用法及代碼示例
- Python Pandas dataframe.sem()用法及代碼示例
- Python Pandas DataFrame.ix[ ]用法及代碼示例
- Python Pandas.Categorical()用法及代碼示例
- Python Pandas.apply()用法及代碼示例
- Python Pandas TimedeltaIndex.contains用法及代碼示例
- Python Pandas Timestamp.now用法及代碼示例
- Python Pandas Series.str.pad()用法及代碼示例
- Python Pandas Series.take()用法及代碼示例
- Python Pandas dataframe.all()用法及代碼示例
- Python Pandas series.str.get()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas tseries.offsets.BusinessDay.rollforward。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。