Python 将时间序列:UNIX历元转换为日期时间

Python 将时间序列:UNIX历元转换为日期时间,python,datetime,pandas,Python,Datetime,Pandas,我打算将以下一系列UNIX时代转换为常规datetime对象: >> val = pd.Series(["1440643875", "1440644191", "1440645638", "1440998720"]) >> val 0 1440643875 1 1440644191 2 1440645638 3 1440998720 Name: obj, dtype: object 似乎有两种方法可以做到这一点。第一个是: >> pd

我打算将以下一系列UNIX时代转换为常规datetime对象:

>> val = pd.Series(["1440643875", "1440644191", "1440645638", "1440998720"])
>> val
0    1440643875
1    1440644191
2    1440645638
3    1440998720
Name: obj, dtype: object
似乎有两种方法可以做到这一点。第一个是:

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

val.astype("datetime64[s]")
TypeError: Cannot parse "1445124547" as unit 's' using casting rule 'same_kind'
这里有什么问题吗


我还尝试用在线Epoch计算器工具检查这些时间戳,它们给出了合理的答案。

问题是元素是字符串,而不是整数。显然,pd.to_datetime不够聪明,无法将字符串转换为datetime

我的解决办法是:

>> 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]

问题是元素是字符串,而不是int。显然,pd.to_datetime不够聪明,无法将字符串转换为datetime

我的解决办法是:

>> 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]
编辑

datetime.datetime.utcfromtimestamp只能获取整数作为参数:

In [510]: datetime.datetime.utcfromtimestamp('1440643875')
TypeError: an integer is required (got type str)
因此,首先需要将序列转换为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]
正如@Adam Smith在评论中指出的,还有datetime.datetime.utcfromtimestamp:

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]
编辑

datetime.datetime.utcfromtimestamp只能获取整数作为参数:

In [510]: datetime.datetime.utcfromtimestamp('1440643875')
TypeError: an integer is required (got type str)
因此,首先需要将序列转换为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]
正如@Adam Smith在评论中指出的,还有datetime.datetime.utcfromtimestamp:

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]

我们可以直接将历元时间转换为日期时间。默认情况下,它将采用%Y-%m-%d%I:%m:%S格式,使用pd.to\u datetime。通过使用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')

我们可以直接将历元时间转换为日期时间。默认情况下,它将采用%Y-%m-%d%I:%m:%S格式,使用pd.to\u datetime。通过使用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')

似乎是显而易见的方法…@AdamSmith Right,但这是用于转换单个元素的。我想做的是转换整个熊猫系列。似乎是显而易见的方法…@AdamSmith Right,但这是用于转换单个元素的。我想做的是转换整个Pandas系列。试着引用系列s的元素,如s=pd.series[1440643875,…],dtype=object。你是对的,首先你需要将其转换为int,然后使用该方法yes,这会起作用。我个人认为链式astype方法更具描述性,也更容易掌握。试着引用s系列的元素,如s=pd.series[1440643875,…],dtype=Objects。你是对的,首先你需要将其转换为int,然后使用该方法yes,这样就行了。我个人认为链式astype方法更具描述性,更容易掌握。