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


要從具有特定時間序列頻率的 DateTimeIndex 中提取微秒,請使用 **DateTimeIndex.microsecond** 屬性。

首先,匯入所需的庫 -

import pandas as pd

建立一個週期為 6 且頻率為 us(即微秒)的 DatetimeIndex。時區為澳大利亞/悉尼 -

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

顯示 DateTimeIndex -

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

獲取微秒 -

print("\nGetting the microseconds..\n",datetimeindex.microsecond)

示例

以下是程式碼 -

import pandas as pd

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

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

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

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

輸出

這將生成以下程式碼 -

DateTimeIndex...
DatetimeIndex([ '2021-10-20 02:30:50+11:00',
'2021-10-20 02:30:50.000001+11:00',
'2021-10-20 02:30:50.000002+11:00',
'2021-10-20 02:30:50.000003+11:00',
'2021-10-20 02:30:50.000004+11:00',
'2021-10-20 02:30:50.000005+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='U')
DateTimeIndex frequency...
<Micro>

Getting the microseconds..
Int64Index([0, 1, 2, 3, 4, 5], dtype='int64')

更新於: 2021年10月18日

170 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.