如何添加 pandas pd.Timestamp
你不能直接添加 Pandas pd.Timestamp 实例:
pandas_timestamp_add_example.py
t1 = pd.Timestamp('now')
t2 = pd.Timestamp('now')
t1 + t2
# TypeError: unsupported operand type(s) for +: 'Timestamp' 和 'Timestamp'但你可以使用它们的 asm8 属性将它们转换为 numpy 时间戳,将该时间戳转换为整数,添加它并转换回来:
pandas_timestamp_asm8_example.py
t1 = pd.Timestamp('now')
t2 = pd.Timestamp('now')
tsum = (t1.asm8.astype(np.int64) + t2.asm8.astype(np.int64))
tsum_timestamp = pd.Timestamp(tsum.astype('<M8[ns]'))If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow