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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39434979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:15:54  来源:igfitidea点击:

AttributeError: 'Timestamp' object has no attribute 'timestamp

pythontimetimestamp

提问by user3449212

While converting a pandaobject to a timestamp, I am facing this strange issue.

在将panda对象转换为时间戳时,我遇到了这个奇怪的问题。

Train['date'] value is like 01/05/2014which I am trying to convert into linuxtimestamp.

Train['date'] 值就像01/05/2014我试图转换为 linuxtimestamp 的值。

My code:

我的代码:

Train = pd.read_csv("data.tsv", sep='\t') # use TAB as column separator
Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())

And I get this error:

我得到这个错误:

Traceback (most recent call last):
  File "socratis.py", line 11, in <module>
    Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
  File "/home/ubuntu/.local/lib/python2.7/site-packages/pandas/core/series.py", line 2220, in apply
    mapped = lib.map_infer(values, f, convert=convert_dtype)
  File "pandas/src/inference.pyx", line 1088, in pandas.lib.map_infer (pandas/lib.c:62658)
  File "socratis.py", line 11, in <lambda>
    Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
AttributeError: 'Timestamp' object has no attribute 'timestamp'

回答by ntg

to_datetime seems to be deprecated.Use to_pydatetime()instead...

to_datetime 似乎已被弃用。使用to_pydatetime(),而不是...

回答by meganaut

The method to_datetime will return a TimeStampinstance. I'm not sure what you are hoping to accomplish by the lambda function, but it appears you are trying to convert some object to a TimeStamp.

to_datetime 方法将返回一个TimeStamp实例。我不确定您希望通过 lambda 函数完成什么,但您似乎正在尝试将某个对象转换为TimeStamp.

Try removing the apply section so it looks like this:

尝试删除应用部分,使其看起来像这样:

Train['timestamp'] = pd.to_datetime(Train['date'])

Train['timestamp'] = pd.to_datetime(Train['date'])

回答by patricktokeeffe

You're looking for datetime.timestamp(), which was added in Python 3.3. Pandas itself isn't involved.

您正在寻找datetime.timestamp(),它是在 Python 3.3 中添加的。熊猫本身不涉及。

N.B..timestamp()will localize naive timestamps to the computer's UTC offset. To the contrary, suggestions in this answer are timezone-agnostic.

NB.timestamp()会将原始时间戳本地化为计算机的 UTC 偏移量。相反,此答案中的建议与时区无关。

Since pandas uses nanoseconds internally(numpy datetime64[ns]), you should be able to do this even with Python 2:

由于 Pandas 在内部使用纳秒(numpy datetime64[ns]),即使使用 Python 2,您也应该能够做到这一点:

Train['timestamp'] = pd.to_datetime(Train['date']).value / 1e9

Or be more explicit wtih something like this (from the datetime docs):

或者更明确地使用这样的东西(来自日期时间文档):

import pandas as pd
from datetime import datetime, timedelta

def posix_time(dt):
    return (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

Train['timestamp'] = pd.to_datetime(Train['date']).apply(posix_time)