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
广告