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

PythonPandas-CustomBusinessDayオフセットオブジェクトを作成します


CustomBusinessDayオフセットオブジェクトを作成するには、Pandasでpd.tseries.offsets.CustomBusinessDay()メソッドを使用します。

まず、必要なライブラリをインポートします-

import pandas as pd

CustomBusinessDayオフセットを作成します。 CustomBusinessDayは、休日を除くカスタム営業日を表すDateOffsetサブクラスです。有効な営業日のウィークマスク-

cbdOffset = pd.tseries.offsets.CustomBusinessDay(n = 5, weekmask = 'Mon Tue Wed Fri')

パンダでタイムスタンプオブジェクトを設定します-

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

タイムスタンプにオフセットを追加し、更新されたタイムスタンプを表示します-

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

以下はコードです-

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-12-31 08:35:10')

# Display the Timestamp
print("Timestamp...\n",timestamp)

# Create the CustomBusinessDay Offset
# CustomBusinessDay is the DateOffset subclass representing custom business days excluding holidays
# Weekmask of valid business days
cbdOffset = pd.tseries.offsets.CustomBusinessDay(n = 5, weekmask = 'Mon Tue Wed Fri')

# Display the CustomBusinessDay Offset
print("\nCustomBusinessDay Offset...\n",cbdOffset)

# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbdOffset)

出力

これにより、次のコードが生成されます-

Timestamp...
 2021-12-31 08:35:10

CustomBusinessDay Offset...
 <5 * CustomBusinessDays>

Updated Timestamp...
 2022-01-10 08:35:10

  1. Pandas-TimestampオブジェクトをネイティブのPython日時オブジェクトに変換します

    TimestampオブジェクトをネイティブのPythondatetimeオブジェクトに変換するには、timestamp.to_pydatetime()メソッドを使用します。 まず、必要なライブラリをインポートします- import pandas as pd パンダでタイムスタンプオブジェクトを作成します timestamp = pd.Timestamp('2021-09-11T13:12:34.261811') タイムスタンプをネイティブPython日時オブジェクトに変換する timestamp.to_pydatetime() 例 以下はコードです import p

  2. Pythonでのタイムスタンプの比較–パンダ

    タイムスタンプを比較するために、インデックス演算子、つまり角かっこを使用できます。まず、必要なライブラリをインポートします- import pandas as pd 3列のデータフレームを作成する- dataFrame = pd.DataFrame(    {       "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"],       &