Dateoffsets是用於Pandas中日期範圍的標準日期增量。就我們傳入的關鍵字args而言,它的工作方式與relativedelta完全相同。DateOffets的工作方式如下,每個偏移量指定一組符合DateOffset的日期。例如,“ Bday”將此集合定義為工作日的日期集合(M-F)。
可以創建DateOffsets以將日期向前移動給定數量的有效日期。例如,可以將Bday(2)添加到日期以將其向前移動兩個工作日。如果日期不是從有效日期開始,則首先將其移動到有效日期,然後創建偏移量。
Pandas tseries.offsets.CustomBusinessHour()
函數用於創建您自己的自定義營業時間。 DateOffset子類,可能表示n個自定義工作日。
用法: pandas.tseries.offsets.CustomBusinessHour()
參數:
n:整型
normalize:將開始/結束日期標準化為午夜,然後再生成日期範圍
weekmask:有效工作日的周掩碼,已傳遞給numpy.busdaycalendar
holidays:沒有
calendar:沒有
start:營業時間開始
end:營業時間結束
offset:datetime.timedelta(0)
返回:抵銷
範例1:采用pandas.tseries.offsets.CustomBusinessHour()
函數來創建您自己的自定義營業時間偏移量。
# importing pandas as pd
import pandas as pd
# Creating Timestamp
ts = pd.Timestamp('2019-4-23 11:15:00')
# Create an offset
cbh = pd.tseries.offsets.CustomBusinessHour(n = 2, weekmask = 'Mon Tue Wed Thu',
start ='12:00')
# Print the Timestamp
print(ts)
# Print the Offset
print(cbh)
輸出:
現在,我們將偏移量添加到給定的timestamp對象中,以增加datetime值。
# Adding the offset to the given timestamp
new_timestamp = ts + cbh
# Print the updated timestamp
print(new_timestamp)
輸出:
從輸出中可以看到,我們已經成功創建了一個偏移並將其添加到給定的時間戳中。
範例2:采用pandas.tseries.offsets.CustomBusinessHour()
函數來創建您自己的自定義營業時間偏移量。
# importing pandas as pd
import pandas as pd
# Creating Timestamp
ts = pd.Timestamp('2019-4-23 11:15:00')
# Create an offset
cbh = pd.tseries.offsets.CustomBusinessHour(start ='07:00', end ='14:00')
# Print the Timestamp
print(ts)
# Print the Offset
print(cbh)
輸出:
現在,我們將偏移量添加到給定的timestamp對象中,以增加datetime值。
# Adding the offset to the given timestamp
new_timestamp = ts + cbh
# Print the updated timestamp
print(new_timestamp)
輸出:
從輸出中可以看到,我們已經成功創建了一個偏移並將其添加到給定的時間戳中。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas tseries.offsets.CustomBusinessHour。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。