如何将 pandas Timedelta 转换为秒

TL;DR

使用

example.py
my_timedelta / np.timedelta64(1, 's')

完整示例

example.py
import pandas as pd
import numpy as np
import time

# 创建 timedelta
t1 = pd.Timestamp("now")
time.sleep(3)
t2 = pd.Timestamp("now")
my_timedelta = t2 - t1

# 将 timedelta 转换为秒
my_timedelta_in_seconds = my_timedelta / np.timedelta64(1, 's')
print(my_timedelta_in_seconds) # 打印 3.00154

Check out similar posts by category: Pandas, Python