Python Pandas - 將 Timedelta 轉換為 NumPy timedelta64
要將 Timedelta 轉換為 NumPy timedelta64,請使用 timedelta.to_timedelta64() 方法。
首先,匯入所需的庫 −
import pandas as pd
建立 Timedelta 物件 −
timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')
顯示 Timedelta −
print("Timedelta...\n", timedelta)將 Timedelta 轉換為 NumPy timedelta64 −
timedelta.to_timedelta64()
範例
以下為程式碼 −
import pandas as pd
# TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s
# create a Timedelta object
timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')
# display the Timedelta
print("Timedelta...\n", timedelta)
# Convert the Timedelta to a NumPy timedelta64.
res = timedelta.to_timedelta64()
# Return the result
print("\nConverting the Timedelta to a NumPy timedelta64....\n", res)輸出
這將生成以下程式碼 −
Timedelta... 2 days 11:22:25.050000045 Converting the Timedelta to a NumPy timedelta64.... 213745050000045 nanoseconds
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP