Python Pandas CustomBusinessHour-オフセットにない場合にのみ、提供された日付を次のオフセットにロールフォワードします
オフセットされていない場合にのみ、提供された日付を次のオフセットにロールフォワードするには、PandasのCustomBusinessHour.rollforward()メソッドを使用します。
まず、必要なライブラリをインポートします-
import pandas as pd
パンダでタイムスタンプオブジェクトを設定します-
timestamp = pd.Timestamp('2021-12-20 08:35:10')
CustomBusinessHourオフセットを作成します。 CustomBusinessHourは、DateOffsetサブクラスです。有効な営業日のウィークマスク-
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 5, weekmask = 'Mon Tue Wed Fri')
タイムスタンプにオフセットを追加し、更新されたタイムスタンプを表示します-
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
オフセットでない場合はロールフォワード-
roll = cbhOffset.rollforward(pd.Timestamp('2021-12-30 08:35:10')) 結果を表示する-
print("\nRoll forward Result...\n",roll)
例
以下はコードです-
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-12-20 08:35:10')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Weekmask of valid business days
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 5, weekmask = 'Mon Tue Wed Fri')
# Display the CustomBusinessHour Offset
print("\nCustomBusinessHour Offset...\n",cbhOffset)
# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
# roll forward if not on offset
roll = cbhOffset.rollforward(pd.Timestamp('2021-12-30 08:35:10'))
# display the result
print("\nRoll forward Result...\n",roll)を表示します 出力
これにより、次のコードが生成されます-
Timestamp... 2021-12-20 08:35:10 CustomBusinessHour Offset... <5 * CustomBusinessHours: CBH=09:00-17:00> Updated Timestamp... 2021-12-20 14:00:00 Roll forward Result... 2021-12-31 09:00:00
-
Pythonでのタイムスタンプの比較–パンダ
タイムスタンプを比較するために、インデックス演算子、つまり角かっこを使用できます。まず、必要なライブラリをインポートします- import pandas as pd 3列のデータフレームを作成する- dataFrame = pd.DataFrame( { "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], &
-
パンダオフセットをPython日付に変換する方法は?
日付オブジェクトからパンダを引くと、パンダのタイムスタンプオブジェクトが得られます。このオブジェクトは、文字列形式の日付またはDateオブジェクト(標準のPython日付)に変換できます。または、日時ライブラリのtimedeltaオブジェクトを使用できます。 例 from pandas.tseries.frequencies import to_offset import pandas as pd dt = pd.to_datetime('2018-01-04') - to_offset("5D") print(type(dt)) 出力 これにより、出力