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

PythonPandas-PeriodIndexオブジェクトから期間の週を取得します


PeriodIndexオブジェクトから期間の週を取得するには、 PeriodIndex.weekを使用します プロパティ。

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

import pandas as pd

PeriodIndexオブジェクトを作成します:-

periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2021-07-15 02:55:15', '2022-06-25 09:40:55'], freq="T")

PeriodIndexオブジェクトを表示-

print("PeriodIndex...\n", periodIndex)

PeriodIndexオブジェクトから週を表示-

print("\nThe week from the PeriodIndex object...\n", periodIndex.week)

以下はコードです-

import pandas as pd

# Create a PeriodIndex object
# PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time
# We have set the frequency using the "freq" parameter
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2021-07-15 02:55:15', '2022-06-25 09:40:55'], freq="T")

# Display PeriodIndex object
print("PeriodIndex...\n", periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency object...\n", periodIndex.freq)

# Display PeriodIndex frequency as string
print("\nPeriodIndex frequency object as a string...\n", periodIndex.freqstr)

# Display week from the PeriodIndex object
print("\nThe week from the PeriodIndex object...\n", periodIndex.week)

出力

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

PeriodIndex...
PeriodIndex(['2021-09-25 07:30', '2019-10-30 04:15', '2021-07-15 02:55', '2022-06-25 09:40'],
dtype='period[T]')

PeriodIndex frequency object...
<Minute>

PeriodIndex frequency object as a string...
T

The week from the PeriodIndex object...
Int64Index([38, 44, 28, 25], dtype='int64')

  1. PythonPandas-Timedeltaオブジェクトから継続時間の合計秒数を取得します

    Timedeltaオブジェクトから継続時間の合計秒数を取得するには、 timedelta.total_seconds()を使用します メソッド。 まず、必要なライブラリをインポートします- import pandas as pd TimeDeltasは、Pythonの標準の日時ライブラリであり、異なる表現のtimedeltaを使用します。Timedeltaオブジェクトを作成します- timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') タイムデルタを表示- print("Time

  2. Python-PandasのTimestampオブジェクトから平日を取得します

    Timestampオブジェクトから平日を取得するには、 timestamp.weekday()を使用します 方法。まず、必要なライブラリをインポートします- import pandas as pd import datetime パンダでタイムスタンプを設定します。タイムスタンプオブジェクトを作成する timestamp = pd.Timestamp(datetime.datetime(2021, 5, 12)) その年の平日を取得します。平日は、月曜日==0、火曜日==1…日曜日==6の数字で表されます。 timestamp.weekday() 例 以下はコードです import