Python Pandas - 從具有特定時間序列頻率的 DateTimeIndex 中提取分鐘


要從具有特定時間序列頻率的 DateTimeIndex 中提取分鐘,請使用 DateTimeIndex.minute 屬性。

首先,匯入所需的庫 -

import pandas as pd

建立一個具有周期 6 和頻率為 T,即 minute 的 DatetimeIndex。時區為澳大利亞/悉尼 -

datetimeindex = pd.date_range('2021-10-20 02:30:55', periods=6, tz='Australia/Sydney', freq='T')

顯示 DateTimeIndex -

print("DateTimeIndex...\n", datetimeindex)

獲取分鐘 -

print("\nGetting the minute..\n",datetimeindex.minute)

範例

以下是程式碼 -

import pandas as pd

# DatetimeIndex with period 6 and frequency as T i.e. minute
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:55', periods=6, tz='Australia/Sydney', freq='T')

# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# get the minute
print("\nGetting the minute..\n",datetimeindex.minute)

輸出

這將生成以下程式碼 -

DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:55+11:00', '2021-10-20 02:31:55+11:00',
'2021-10-20 02:32:55+11:00', '2021-10-20 02:33:55+11:00',
'2021-10-20 02:34:55+11:00', '2021-10-20 02:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='T')
DateTimeIndex frequency...
<Minute>

Getting the minute..
Int64Index([30, 31, 32, 33, 34, 35], dtype='int64')

更新於: 18-Oct-2021

938 次瀏覽

Kickstart Your Career

完成教程以獲得認證

立即開始
廣告
© . All rights reserved.