PythonPandasCustomBusinessHour-提供された日付をさかのぼってロール
提供された日付を逆方向にロールバックするには、PandasのCustomBusinessHour.rollback()メソッドを使用します。まず、必要なライブラリをインポートします-
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_back = cbhOffset.rollback(pd.Timestamp('2021-12-18 08:35:10')) 結果を表示する-
print("\nRoll Backward Result...\n",roll_back)
例
以下はコードです-
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 backward if not on offset
roll_back = cbhOffset.rollback(pd.Timestamp('2021-12-18 08:35:10'))
# display the result
print("\nRoll Backward Result...\n",roll_back)を表示します 出力
これにより、次のコードが生成されます-
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 Backward Result... 2021-12-17 17:00:00
-
Pythonで日付文字列をタイムスタンプに変換する
文字列をタイムスタンプに変換する必要がある場合は、「mktime」メソッドが使用されます。このメソッドは「時間」パッケージに含まれています。 以下は同じのデモンストレーションです- 例 import time import datetime my_string = "24/03/2021" print("The date string is :") print(my_string) print("The timestamp is :") print(time.mktime(datetime.datetime.strptime(my
-
パンダオフセットを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)) 出力 これにより、出力