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

PythonPandas-BusinessHourオフセットを作成する


BusinessHourオフセットを作成するには、Pandasでpd.tseries.offsets.BusinessHour()メソッドを使用します。まず、必要なライブラリをインポートします-

import pandas as pd

BusinessHourオフセットを作成します。 BusinessHourはDateOffsetサブクラスです。ここで、「開始」は、24時間形式のカスタム営業時間の開始時刻です。 「終了」は、24時間形式のカスタム営業時間の終了時刻です-

bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00")

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

timestamp = pd.Timestamp('2021-1-1 01:55:30')

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

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

以下はコードです-

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-1-1 01:55:30')

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

# Create the BusinessHour Offset
# BusinessHour is the DateOffset subclass
# Here, "start" is the start time of your custom business hour in 24h format.
# The "end" is the end time of your custom business hour in 24h format.
bhOffset = pd.tseries.offsets.BusinessHour(start="09:30", end = "18:00")

# Display the BusinessHour Offset
print("\nBusinessHour Offset...\n",bhOffset)

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

出力

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

Timestamp...
 2021-01-01 01:55:30

BusinessHour Offset...
 <BusinessHour: BH=09:30-18:00>

Updated Timestamp...
 2021-01-01 10:30:00

  1. 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

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

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