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

PythonPandas-期間の時間コンポーネントを取得します


期間の時間コンポーネントを取得するには、 period.hourを使用します プロパティ。

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

import pandas as pd

pandas.Periodは期間を表します。 2つのPeriodオブジェクトの作成-

period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="H", year = 2021, month = 7, day = 16, hour = 2, minute = 35)

2つのPeriodオブジェクトから1日の時間を取得します-

res1 = period1.hour
res2 = period2.hour

以下はコードです-

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="H", year = 2021, month = 7, day = 16, hour = 2, minute = 35)

# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

# get the hour of the day from two Period objects
res1 = period1.hour
res2 = period2.hour

# Return the hour of the day from the two Period objects
print("\nHour of the day from the 1st Period object ...\n", res1)
print("\nHour of the day from the 2nd Period object...\n", res2)

出力

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

Period1...
2020-09-23 05:55:30

Period2...
2021-07-16 02:00

Hour of the day from the 1st Period object ...
5

Hour of the day from the 2nd Period object...
2

  1. PythonPandas-期間が含まれる曜日を取得します

    期間が該当する曜日を取得するには、 period.dayofweekを使用します プロパティ まず、必要なライブラリをインポートします- import pandas as pd pandas.Periodは期間を表します。 2つのPeriodオブジェクトの作成- period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55) 期間オブジェクトを表示する- print(&quo

  2. PythonPandas-生理が始まる月の日を取得します

    期間が該当する月の日を取得するには、 period.dayを使用します プロパティ。 まず、必要なライブラリをインポートします- import pandas as pd pandas.Periodは期間を表します。 2つのPeriodオブジェクトの作成- period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55) 2つのPeriodオブジェクトから月の日を取得します- r