我有变量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/