เพื่อแปลง Timedelta เป็น NumPy timedelta64 ให้ใช้เมธอด timedelta.to_timedelta64()
เริ่มต้นด้วยการนำเข้าไลบรารีที่จำเป็น:
import pandas as pd
สร้าง Timedelta object:
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