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

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


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

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

import pandas as pd

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

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

CustomBusinessHourオフセットを作成します。 CustomBusinessHourは、DateOffsetサブクラスです。有効な営業日のウィークマスク-

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

CustomBusinessHourオフセットを表示する-

print("\nCustomBusinessHour Offset...\n",cbhOffset)

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

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

以下はコードです-

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 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)

出力

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

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

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

Updated Timestamp...
 2021-12-31 14:00:00

  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"],       &