gpt4 book ai didi

python - 'Timestamp' 对象没有属性 'timestamp'

转载 作者:太空宇宙 更新时间:2023-11-04 05:28:28 25 4
gpt4 key购买 nike

我正在学习一门类(class),其中我必须将日期转换为 unix 时间戳。

import pandas as pd

df = pd.read_csv('file.csv')
print type(df.iloc[-1].name)

类 'pandas.tslib.Timestamp'

ts = df.iloc[-1].name.timestamp()

AttributeError: 'Timestamp' 对象没有属性 'timestamp'

最佳答案

你实际上并没有问一个问题(下次提示:更明确),但我假设你想要一个来自 Pandas Timestamp 对象的纪元/Unix 时间戳。

如果您使用 pandas.tslib.Timestamp.value 方法,您将返回以微秒(1/1,000,000 秒)为单位的时间戳:

In [1]: import pandas as pd

In [2]: date_example = pd.to_datetime("2016-06-21")

In [3]: type(date_example)
Out[3]: pandas.tslib.Timestamp

In [4]: date_example.value
Out[4]: 1466467200000000000

如果您愿意,您可以简单地除以 1000 得到毫秒或 1000000 得到整秒,例如:

In [5]: date_example.value / 1000000
Out[5]: 1466467200000

关于python - 'Timestamp' 对象没有属性 'timestamp',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37945430/

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