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

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


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

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

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="S")

PeriodIndexオブジェクトを表示-

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

PeriodIndexオブジェクトからの秒数を表示-

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

以下はコードです-

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="S")

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

出力

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

PeriodIndex...
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'],
dtype='period[S]')

PeriodIndex frequency object...
<Second>

PeriodIndex frequency object as a string...
S

The seconds from the PeriodIndex object...
Int64Index([35, 45, 15, 55], dtype='int64')

  1. PythonPandas-Timedeltaオブジェクトから秒を返します

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

  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