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

标签 python pandas timestamp integer

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

相关文章:

python - Tkinter - Spinbox 看起来比预期的要长

mysql - 使用 JOOQ 查询 Unix 时间戳

python - "*_"是在 Python 中忽略参数的可接受方式吗?

python - 如何在 Python 3.8+ 和 Python 2.7 中使用 collections.abc

python - 使用 pandas GroupBy 聚合时设置 MultiIndex

python - ipython pylab : print histogram from dictionary

python - 重组数据框

可变时间范围内的Mysql最大行数

php - 按小时选择摘要中的行并将其分组

python - python中成员函数的默认值