PythonPandas-CustomBusinessHourオフセットに適用されるウィークマスクを取得します
CustomBusinessHourオフセットに適用されるウィークマスクを取得するには、PandasのCustomBusinessHour.weekmaskプロパティを使用します。
まず、必要なライブラリをインポートします-
import pandas as pd
パンダでタイムスタンプオブジェクトを設定します-
timestamp = pd.Timestamp('2021-11-14 05:20:30')
CustomBusinessHourオフセットを作成します。 CustomBusinessHourは、DateOffsetサブクラスです。有効な営業日のウィークマスク-
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 7, weekmask = 'Mon Tue Wed Fri')
タイムスタンプにオフセットを追加し、更新されたタイムスタンプを表示します-
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
ウィークマスクを表示する-
print("\nThe weekmask on the CustomBusinessHour object..\n", cbhOffset.weekmask) 例
以下はコードです-
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# 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 = 7, 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)
# Return frequency applied on the given CustomBusinessHour Offset object as a string
print("\nFrequency applied on the given CustomBusinessHour Offset object...\n",cbhOffset.freqstr)
# display the weekmask
print("\nThe weekmask on the CustomBusinessHour object..\n", cbhOffset.weekmask) 出力
これにより、次のコードが生成されます-
Timestamp... 2021-11-14 05:20:30 CustomBusinessHour Offset... <7 * CustomBusinessHours: CBH=09:00-17:00> Updated Timestamp... 2021-11-15 16:00:00 Frequency applied on the given CustomBusinessHour Offset object... 7CBH The weekmask on the CustomBusinessHour object.. Mon Tue Wed Fri
-
Python-PandasのTimestampオブジェクトから平日を取得します
Timestampオブジェクトから平日を取得するには、 timestamp.weekday()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd import datetime パンダでタイムスタンプを設定します。タイムスタンプオブジェクトを作成する timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) その年の平日を取得します。平日は、月曜日==0、火曜日==1…日曜日==6の数字で表されます。 timestamp.weekday() 例 以下はコードです import
-
Pythonパンダ-UTCオフセット時間を取得
UTCオフセット時間を取得するには、 timestamp.utcoffset()を使用します 。まず、必要なライブラリをインポートします- import pandas as pd タイムスタンプの作成 timestamp = pd.Timestamp('2021-10-16T15:12:34.261811624', tz='UTC') UTCの日時を含む新しいタイムスタンプ timestamp.utcnow() UTCオフセット時間を取得する timestamp.utcoffset() 例 以下はコードです import pandas as pd