如何从两个时间戳创建 pandas.Timedelta 对象
如果你有两个 pandas.Timestamp 对象,你可以简单地使用减号运算符(-)将它们相减以获得 pandas.Timedelta 对象:
pandas_timedelta_example.py
import pandas as pd
import time
# 创建两个时间戳
ts1 = pd.Timestamp.now()
time.sleep(2)
ts2 = pd.Timestamp.now()
# 这些时间戳的差是 pandas.Timedelta 对象。
timedelta = ts2 - ts1
print(timedelta) # 打印 '0 days 00:00:02.000752'If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow