如何将 pandas pd.Timestamp() 转换为 epoch 时间戳

要将 Pandas pd.Timestamp 转换为表示自 epoch 以来秒数的数字,使用 ts.timestamp()

timestamp_to_epoch.py
import pandas as pd

# Example pandas timestamp
timestamp = pd.Timestamp('2024-05-28 12:34:56')

# Convert to epoch timestamp with second resolution
epoch_timestamp_ms = timestamp.timestamp() # 1716899696.0

由于这返回一个浮点数,你可以将其乘以 1000 来获得毫秒分辨率的时间戳:

timestamp_to_epoch_ms.py
import pandas as pd

# Example pandas timestamp
timestamp = pd.Timestamp('2024-05-28 12:34:56')

# Convert to epoch timestamp with second resolution
epoch_timestamp_ms = timestamp.timestamp() * 1000. # 1716899696000.0

Check out similar posts by category: Pandas, Python