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

PythonPandas-PeriodIndexオブジェクトから年を取得します


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

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

import pandas as pd

PeriodIndexオブジェクトを作成します。 「freq」パラメータを使用して周波数を設定しました-

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("\nPeriodIndex frequency object...\n", periodIndex.freq)

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

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

以下はコードです-

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 year from the PeriodIndex object
print("\nThe year from the PeriodIndex object...\n", periodIndex.year)

出力

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

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 year from the PeriodIndex object...
Int64Index([2021, 2019, 2021, 2022], dtype='int64')

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

  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() 例 以下はコ