Để chuyển đổi Timedelta thành NumPy hẹn giờelta64, hãy sử dụng timedelta.to_timedelta64 () phương pháp.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
import pandas as pd
Tạo một đối tượng Timedelta -
timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')
Hiển thị Timedelta -
print("Timedelta...\n", timedelta) Chuyển đổi đồng hồ Timedelta thành đồng hồ hẹn giờ NumPy64 -
timedelta.to_timedelta64()
Ví dụ
Sau đây là mã -
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) Đầu ra
Điều này sẽ tạo ra mã sau -
Timedelta... 2 days 11:22:25.050000045 Converting the Timedelta to a NumPy timedelta64.... 213745050000045 nanoseconds