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


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

首先,匯入必需的庫 −

import pandas as pd

建立一個週期為 6、頻率為 ns(即納秒)的 DatetimeIndex −

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

顯示 DateTimeIndex −

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

獲取納秒 −

print("\nGetting the nanoseconds..\n",datetimeindex.nanosecond)

案例

以下為程式碼 −

import pandas as pd

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

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

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

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

輸出

這將產生以下程式碼 −

DateTimeIndex...
DatetimeIndex([ '2021-10-20 02:30:50+11:00',
'2021-10-20 02:30:50.000000001+11:00',
'2021-10-20 02:30:50.000000002+11:00',
'2021-10-20 02:30:50.000000003+11:00',
'2021-10-20 02:30:50.000000004+11:00',
'2021-10-20 02:30:50.000000005+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='N')
DateTimeIndex frequency...
<Nano>

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

更新時間: 2021 年 10 月 18 日

515 次瀏覽

開啟你的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.