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

PythonPandas-CustomBusinessHourオフセットオブジェクトからカスタム営業時間の終了時刻を24時間形式で表示します


CustomBusinessHourオフセットオブジェクトからカスタム営業時間の終了時刻を24時間形式で表示するには、PandasのCustomBusinessHour.endプロパティを使用します。

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

import pandas as pd

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

timestamp = pd.Timestamp('2021-11-14 05:20:30')

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

cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30", n = 5)

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

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

カスタム営業時間の終了時刻を表示する-

print("\nThe end time of the custom business hour...\n",cbhOffset.end)

以下はコードです-

import pandas as pd

# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-11-14 05:20:30')

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

# Create the CustomBusinessHour Offset
# CustomBusinessHour 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.
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30", n = 5)

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

# Display the end time of the custom business hour
print("\nThe end time of the custom business hour...\n",cbhOffset.end)

出力

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

Timestamp...
 2021-11-14 05:20:30

CustomBusinessHour Offset...
 <5 * CustomBusinessHours: CBH=09:30-18:30>

Updated Timestamp...
 2021-11-15 14:30:00

The end time of the custom business hour...
 (datetime.time(18, 30),)

  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. PythonPandas-Timestampオブジェクトから現在の日付と時刻を取得します

    Timestampオブジェクトから現在の日付と時刻を取得し、 timestamp.today()を使用します メソッド。 まず、必要なライブラリをインポートします- import pandas as pd import datetime パンダでタイムスタンプを作成する timestamp = pd.Timestamp(datetime.datetime(2021, 10, 10)) タイムスタンプを表示する print("Timestamp: ", timestamp) 現在の日付と時刻を取得する res = timestamp.today() 例 以下はコ