gpt4 book ai didi

python - 使用 astype(int) 将时间戳转换为 int

转载 作者:行者123 更新时间:2023-12-02 16:16:38 24 4
gpt4 key购买 nike

我有变量time:

>>> time = pd.to_datetime(1613260800000000000)
>>> time
Timestamp('2021-02-14 00:00:00')

time 是一个时间戳。现在我想将 time 转换回 int 值。但是 Timestamp 对象没有属性 astype。运行以下代码:

>>>time.astype(int)

AttributeError: 'Timestamp' object has no attribute 'astype'

我知道对于数据框的一列我可以这样做:

>>> df['start_time'] = df['start_time'].to_timedate()
>>> df['start_time'] = df['start_time'].astype(int)

第二个命令将列的类型分配给 int

但我没有找到全面的资源来解释如何处理单个 Timestamp 参数。

我该如何解决这个问题?

最佳答案

只需使用time_variable.timestamp()

但这将以秒为单位......而不是 ms 或其他任何东西(我们?)

您需要将结果乘以 1e9 才能得到与您输入的值相同的值

关于python - 使用 astype(int) 将时间戳转换为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66487651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com