Python
 Computer >> コンピューター >  >> プログラミング >> Python

Python Pandas入門:CustomBusinessHourオブジェクトに適用されたキーワード引数を表示する方法

Pandasで特定の CustomBusinessHour オブジェクトに適用されているキーワード引数を確認したい場合は、kwds プロパティを使用します。このプロパティは、オフセットの作成時に指定した引数(営業日の weekmask、休日、開始時刻・終了時刻など)を辞書形式で返してくれるため、オフセットの設定内容を簡単に把握できます。

手順1:必要なライブラリをインポートする

まず、Pandasをインポートします。

import pandas as pd

手順2:タイムスタンプオブジェクトを作成する

次に、基準となるタイムスタンプを作成します。

timestamp = pd.Timestamp('2021-10-25 08:35:10')

手順3:CustomBusinessHourオフセットを作成する

CustomBusinessHour は DateOffset のサブクラスで、カスタマイズ可能な営業時間・営業日に基づいて日時をずらすことができます。ここでは、以下の2つの引数を指定しています。

  • n = 3:3営業時間分だけオフセットを適用する
  • weekmask = 'Mon Tue Wed Fri Sat':月・火・水・金・土を有効な営業日として設定する
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n=3, weekmask='Mon Tue Wed Fri Sat')

手順4:オフセットを適用して結果を確認する

作成したオフセットをタイムスタンプに加算すると、更新後の日時が得られます。

print("\nUpdated Timestamp...\n", timestamp + cbhOffset)

手順5:キーワード引数を表示する

最後に、kwds プロパティを使って、対象の CustomBusinessHour オフセットに適用されたキーワード引数を出力します。

print("\nKeyword arguments on the given CustomBusinessHour Offset...\n", cbhOffset.kwds)

完全なサンプルコード

以下に、ここまでの手順をまとめた完全なコードを示します。

import pandas as pd

# Pandasでタイムスタンプオブジェクトを設定
timestamp = pd.Timestamp('2021-10-25 08:35:10')

# タイムスタンプを表示
print("Timestamp...\n", timestamp)

# CustomBusinessHourオフセットを作成
# CustomBusinessHourはDateOffsetのサブクラス
# weekmaskで有効な営業日を指定
cbhOffset = pd.tseries.offsets.CustomBusinessHour(n=3, weekmask='Mon Tue Wed Fri Sat')

# CustomBusinessHourオフセットを表示
print("\nCustomBusinessHour Offset...\n", cbhOffset)

# オフセットをタイムスタンプに加算し、更新後のタイムスタンプを表示
print("\nUpdated Timestamp...\n", timestamp + cbhOffset)

# 適用された頻度を文字列として取得
print("\nFrequency applied on the given CustomBusinessHour Offset object...\n", cbhOffset.freqstr)

# キーワード引数を表示
print("\nKeyword arguments on the given CustomBusinessHour Offset...\n", cbhOffset.kwds)

実行結果

上記のコードを実行すると、以下のような出力が得られます。

Timestamp...
2021-10-25 08:35:10

CustomBusinessHour Offset...
<3 * CustomBusinessHours: CBH=09:00-17:00>

Updated Timestamp...
2021-10-25 12:00:00

Frequency applied on the given CustomBusinessHour Offset object...
3CBH

Keyword arguments on the given CustomBusinessHour Offset...
{'weekmask': 'Mon Tue Wed Fri Sat', 'holidays': (), 'calendar': <numpy.busdaycalendar object at 0x7f48facfa840>, 'start': (datetime.time(9, 0),), 'end': (datetime.time(17, 0),), 'offset': datetime.timedelta(0)}

出力結果の解説

kwds プロパティの出力には、以下の情報が含まれています。

  • weekmask:有効な営業日の一覧(月・火・水・金・土)
  • holidays:指定された休日のタプル(今回は空)
  • calendar:NumPyの busdaycalendar オブジェクト
  • start / end:営業時間の開始時刻(9:00)と終了時刻(17:00)
  • offset:追加のオフセット値(デフォルトは0)

また、タイムスタンプ「2021-10-25 08:35:10」は営業開始時刻(9:00)より前であるため、3営業時間分のオフセットを加算すると「2021-10-25 12:00:00」になります。これは、営業開始の9:00から起算して3時間後の時刻です。freqstr の出力「3CBH」からも、3単位の CustomBusinessHour オフセットが適用されていることが確認できます。

  1. Python Pandas – 指定したDateOffsetオブジェクトに適用された頻度を返す方法

    Pandasで指定されたDateOffsetオブジェクトに適用されている頻度を取得するには、offset.freqstr属性を使用します。この属性は、DateOffsetオブジェクトに設定された頻度を「5D」のような文字列形式で返してくれます。また、to_offset()関数を使うことで、頻度を表す文字列からDateOffsetオブジェクトを簡単に生成できます。 まず、必要なライブラリをインポートしましょう。 from pandas.tseries.frequencies import to_offset import pandas as pd 次に、Pandasでタイムスタンプオブジェクトを

  2. Python Pandas – 指定されたDateOffsetオブジェクトに適用される頻度を文字列として取得する方法

    指定された DateOffset オブジェクトに適用されている頻度を文字列として取得するには、Pandas の offset.freqstr プロパティを使用します。このプロパティを使うことで、DateOffset オブジェクトに設定された頻度の情報を簡単に確認できます。必要なライブラリのインポートまず、必要なライブラリをインポートします。from pandas.tseries.offsets import DateOffset import pandas as pdタイムスタンプオブジェクトの設定Pandas でタイムスタンプオブジェクトを設定します。timestamp = pd.Times