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

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


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

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

import pandas as pd

PeriodIndexオブジェクトを作成します。 PeriodIndexは、一定期間を示す順序値を保持する不変のndarrayです-

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オブジェクトから曜日を表示します。月曜日=0、火曜日=1...日曜日=6-

の曜日
print("\nThe day of week from the PeriodIndex object...\n", periodIndex.weekday)

以下はコードです-

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)

# Display day of the week from the PeriodIndex object
# The day of the week with Monday=0, Tuesday=1 ... Sunday=6
print("\nThe day of week from the PeriodIndex object...\n", periodIndex.weekday)
の曜日

出力

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

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

The day of week from the PeriodIndex object...
Int64Index([5, 2, 3, 5], dtype='int64')

  1. PythonPandas-整数入力を使用してTimedeltaオブジェクトから秒を取得します

    Timedeltaオブジェクトから秒を返すには、 timedelta.secondsを使用します 財産。まず、必要なライブラリをインポートします- import pandas as pd TimeDeltasは、Pythonの標準の日時ライブラリであり、異なる表現のtimedeltaを使用します。単位sを使用して秒の整数入力を設定します。 Timedeltaオブジェクトを作成する timedelta = pd.Timedelta(50, unit ='s') タイムデルタを表示する print("Timedelta...\n", timedelta)

  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