将 Pandas 时间序列:UNIX 纪元转换为日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34078114/
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-09-14 00:19:20  来源:igfitidea点击:

Convert Pandas time series: UNIX epoch to datetime

pythondatetimepandas

提问by user1496984

I'm tying to convert the following series of UNIX epochs to regular datetime objects:

我打算将以下系列的 UNIX 纪元转换为常规日期时间对象:

>> val = pd.Series(["1440643875", "1440644191", "1440645638", "1440998720"])
>> val
0    1440643875
1    1440644191
2    1440645638
3    1440998720
Name: obj, dtype: object

There appears to be two ways of doing this. The first is:

似乎有两种方法可以做到这一点。第一个是:

>> pd.to_datetime(val, unit='s')
ValueError: year is out of range

And the second:

第二个:

val.astype("datetime64[s]")
TypeError: Cannot parse "1445124547" as unit 's' using casting rule 'same_kind'

What seems to be the problem here?

这里似乎有什么问题?

I also tried checking these timestamps with the "Online Epoch Calculator" tools, and they give out reasonable answers..

我还尝试使用“在线纪元计算器”工具检查这些时间戳,他们给出了合理的答案。

回答by user1496984

The issue was that the elements were strings, and not ints. Apparently, pd.to_datetime()isn't smart enough to convert from strings to datetime.

问题是元素是字符串,而不是整数。显然,pd.to_datetime()从字符串转换为日期时间不够聪明。

My solution was this:

我的解决方案是这样的:

>> val.astype('int').astype("datetime64[s]")
0   2015-08-27 02:51:15
1   2015-08-27 02:56:31
2   2015-08-27 03:20:38
3   2015-08-31 05:25:20
dtype: datetime64[ns]

回答by Anton Protopopov

EDITED

已编辑

datetime.datetime.utcfromtimestampcould get only integer as paramer:

datetime.datetime.utcfromtimestamp只能得到整数作为参数:

In [510]: datetime.datetime.utcfromtimestamp('1440643875')
TypeError: an integer is required (got type str)

So first you need to convert your Series to int then you could use these methods:

所以首先你需要将你的系列转换为 int 然后你可以使用这些方法:

import pandas as pd
import datetime 

s = pd.Series(["1440643875", "1440644191", "1440645638", "1440998720"], dtype=object)

s = pd.to_numeric(s)

In [50]: s
Out[50]:
0    1440643875
1    1440644191
2    1440645638
3    1440998720
dtype: int64

In [51]: pd.to_datetime(s, unit='s')
Out[51]:
0   2015-08-27 02:51:15
1   2015-08-27 02:56:31
2   2015-08-27 03:20:38
3   2015-08-31 05:25:20
dtype: datetime64[ns]

Also datetime.datetime.utcfromtimestampas @Adam Smith pointed out in comment:

同样datetime.datetime.utcfromtimestamp正如@Adam Smith 在评论中指出的那样:

In [52]: s.apply(datetime.datetime.utcfromtimestamp)
Out[52]:
0   2015-08-27 02:51:15
1   2015-08-27 02:56:31
2   2015-08-27 03:20:38
3   2015-08-31 05:25:20
dtype: datetime64[ns]

回答by SANJANA KABADI

We can directly convert the epoch time to datetime. By default it will be in %Y-%m-%d %I:%M:%S format by using pd.to_datetime. By using dt.strftime complete column can be formatted in the required format.

我们可以直接将纪元时间转换为日期时间。默认情况下,它将使用 pd.to_datetime 以 %Y-%m-%d %I:%M:%S 格式。通过使用 dt.strftime 可以将完整的列格式化为所需的格式。

from datetime import datetime as dt
import pandas as pd
input_data_df['timestamp']=pd.to_datetime(input_data_df['epoch'],unit='ms')
input_data_df['timestamp'] = input_data_df['timestamp'].dt.strftime('%d-%m-%Y %I:%M:%S')