【问题标题】:Converting UTC timestamps to seconds TypeError将 UTC 时间戳转换为秒 TypeError
【发布时间】:2019-03-23 20:57:09
【问题描述】:

我有以下熊猫系列数据::

print(times)

给予

1        2017-07-23T00:26:50Z
3        2017-07-31T04:07:24Z
6        2017-07-18T15:09:20Z
8        2017-07-12T05:24:14Z
...
12353    2017-07-12T18:34:48Z
12355    2017-08-01T22:27:42Z
12356    2017-07-11T03:36:44Z

我只是想将这些 UTC 时间戳转换为秒或

from astropy.time import Time
t = pd.Timestamp(times)

只是给出一个

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-320d28559d92> in <module>()
----> 1 t = pd.Timestamp(times)
pandas/_libs/tslibs/timestamps.pyx in 
pandas._libs.tslibs.timestamps.Timestamp.__new__()
pandas/_libs/tslibs/conversion.pyx in 
pandas._libs.tslibs.conversion.convert_to_tsobject()

TypeError: Cannot convert input [1        2017-07-23T00:26:50Z

我真的不明白发生了什么。

【问题讨论】:

  • 时间戳前的第一个数字是多少? -->1 2017-07-23T00:26:50Z
  • 我认为这只是 pandas/jupyter 的行号。
  • 我不知道如何摆脱它......

标签: python pandas timestamp unix-timestamp utc


【解决方案1】:

您可以使用 Pandas DateTime 功能:

import pandas as pd

import pandas as pd
s=pd.Series(['2017-07-23T00:26:50Z', '2017-07-31T04:07:24Z', '2017-07-18T15:09:20Z', '2017-07-12T18:34:48Z'])

pd.to_datetime(s)

附: - 那些“额外的数字”只是当您读取数据时 pandas 分配的行号/自动索引 :)

【讨论】:

  • 这不起作用。我可以像上面那样做 one 日期时间,但是给 to_datetime 整个系列会导致崩溃
  • 您应该添加一个可重现的问题示例。上面的代码工作得很好......如果我上面的解决方案对你不起作用,我敢打赌你的列表中某处有一个奇怪的项目;我建议循环并查看它在哪里引发错误。如果人们无法自己重新创建问题来解决,他们将很难提供帮助:)
  • 老实说,我不知道如何在没有例如问题的情况下给您一个可重现的问题示例将代码和数据链接到网页/GitHub。
  • 另外,您没有在原始问题代码中使用 pandas 日期时间...所以尝试使用它,看看它是否可以解决问题(您正在使用 pd.Timestamp
猜你喜欢
  • 1970-01-01
  • 2019-08-15
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
相关资源
最近更新 更多