Python Pandas – 指定したCustomBusinessHourオブジェクトに適用されたルールコードを取得する方法
指定されたCustomBusinessHourオブジェクトに適用されているルールコードを取得するには、PandasのCustomBusinessHour.rule_codeプロパティを使用します。このプロパティは、そのオフセットが表す周波数(頻度)のコードを文字列として返します。
必要なライブラリのインポート
まず、必要なライブラリをインポートします。
import pandas as pd
タイムスタンプオブジェクトの作成
Pandasでタイムスタンプオブジェクトを設定します。
timestamp = pd.Timestamp('2021-11-14 05:20:30')CustomBusinessHourオフセットの作成
CustomBusinessHourオフセットを作成します。CustomBusinessHourはDateOffsetのサブクラスであり、営業時間ベースの日時計算を行う際に便利です。weekmaskパラメータで有効な営業日を指定し、normalizeパラメータで結果の日付を正規化できます。
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 4, weekmask = 'Mon Tue Wed Fri Sat' ,normalize=True)
タイムスタンプへのオフセット適用
タイムスタンプにオフセットを加算し、更新後のタイムスタンプを表示します。
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)ルールコードの取得
指定されたCustomBusinessHourオフセットに適用された周波数のルールコードを返します。
print("\nThe rule code of the CustomBusinessHour object..\n", cbhOffset.rule_code)サンプルコード全体
以下は完全なサンプルコードです。
import pandas as pd
# Pandasでタイムスタンプオブジェクトを設定
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# タイムスタンプを表示
print("Timestamp...\n",timestamp)
# CustomBusinessHourオフセットを作成
# CustomBusinessHourはDateOffsetのサブクラス
# weekmaskで有効な営業日を指定
# "normalize"パラメータを使用して正規化
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n = 4, weekmask = 'Mon Tue Wed Fri Sat' ,normalize=True)
# CustomBusinessHourオフセットを表示
print("\nCustomBusinessHour Offset...\n",cbhOffset)
# タイムスタンプにオフセットを加算し、更新後のタイムスタンプを表示
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
# 指定されたCustomBusinessHourオフセットオブジェクトに適用された周波数を文字列として返す
print("\nFrequency applied on the given CustomBusinessHour Offset object...\n",cbhOffset.freqstr)
# 指定されたCustomBusinessHourオフセットに適用された周波数のルールコードを返す
print("\nThe rule code of the CustomBusinessHour object..\n", cbhOffset.rule_code)実行結果
上記のコードを実行すると、次のような出力が得られます。
Timestamp... 2021-11-14 05:20:30 CustomBusinessHour Offset... <4 * CustomBusinessHours: CBH=09:00-17:00> Updated Timestamp... 2021-11-15 00:00:00 Frequency applied on the given CustomBusinessHour Offset object... 4CBH The rule code of the CustomBusinessHour object.. CBH
このように、rule_codeプロパティを使用することで、CustomBusinessHourオブジェクトに適用された周波数のルールコード「CBH」を簡単に確認できます。また、freqstrプロパティを使えば、「4CBH」のようにオフセット数を含む周波数文字列も取得可能です。
-
Python Pandas – 指定されたDateOffsetオブジェクトのナノ秒数を取得する方法
Pandasで指定されたDateOffsetオブジェクトに含まれるナノ秒数を取得するには、offset.nanosプロパティを使用します。このプロパティは、オフセットが固定長(ナノ秒で表現可能な期間)である場合に、そのナノ秒数を整数値として返します。必要なライブラリのインポートまず、必要なライブラリをインポートします。from pandas.tseries.frequencies import to_offset import pandas as pdタイムスタンプの作成Pandasでタイムスタンプオブジェクトを設定します。timestamp = pd.Timestamp(2021-08-30
-
Python Pandas – 指定されたDateOffsetオブジェクトに適用される頻度を文字列として取得する方法
指定された DateOffset オブジェクトに適用されている頻度を文字列として取得するには、Pandas の offset.freqstr プロパティを使用します。このプロパティを使うことで、DateOffset オブジェクトに設定された頻度の情報を簡単に確認できます。必要なライブラリのインポートまず、必要なライブラリをインポートします。from pandas.tseries.offsets import DateOffset import pandas as pdタイムスタンプオブジェクトの設定Pandas でタイムスタンプオブジェクトを設定します。timestamp = pd.Times