Python Pandas——如何利用秒頻率對 DateTimeIndex 進行舍入
要對 DateTimeIndex 進行秒頻率舍入,請使用 DateTimeIndex.round() 方法。對於秒頻率,請將 freq 引數值設定為 ‘S’。
首先,匯入必需的庫 −
import pandas as pd
使用週期為 5 且頻率為 s(即秒)建立 DatetimeIndex −
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')對日期為秒頻率的 DateTimeIndex 執行舍入運算。對於秒頻率,我們使用了 'S' −
print("\nPerforming round operation with seconds frequency...\n",
datetimeindex.round(freq='S'))示例
以下是程式碼 −
import pandas as pd
# DatetimeIndex with period 5 and frequency as s i.e. seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
# display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# getting the second
res = datetimeindex.second
# display only the second
print("\nThe second from DateTimeIndex...\n", res)
# Round operation on DateTimeIndex date with seconds frequency
# For seconds frequency, we have used 'S'
print("\nPerforming round operation with seconds frequency...\n",
datetimeindex.round(freq='S'))輸出
這將生成以下程式碼 −
DateTimeIndex... DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30', '2021-09-29 07:21:00.261811624+09:30', '2021-09-29 07:21:28.261811624+09:30', '2021-09-29 07:21:56.261811624+09:30', '2021-09-29 07:22:24.261811624+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='28S') DateTimeIndex frequency... <28 * Seconds> The second from DateTimeIndex... Int64Index([32, 0, 28, 56, 24], dtype='int64') Performing round operation with seconds frequency... DatetimeIndex(['2021-09-29 07:20:32+09:30', '2021-09-29 07:21:00+09:30', '2021-09-29 07:21:28+09:30', '2021-09-29 07:21:56+09:30', '2021-09-29 07:22:24+09:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP