Python 将timestamp转换为pandas.Series中的datetime.datetime

Python 将timestamp转换为pandas.Series中的datetime.datetime,python,datetime,pandas,timestamp,series,Python,Datetime,Pandas,Timestamp,Series,我有一个熊猫系列,其中索引是一个整数列表(时间戳),如何将它们转换为datetime.datetime(带时区)比原始转换效率更高 pd.Series(data=s.values, index=map(lambda x:datetime.datetime.fromtimestamp(x,tz=utc), s.index)) 使用来指定日期时间,您可以提供一个单位来选择整数的含义 In [50]: pd.to_datetime(s,unit='s') Out[50]: 0 1970-01-

我有一个熊猫系列,其中索引是一个整数列表(时间戳),如何将它们转换为datetime.datetime(带时区)比原始转换效率更高

pd.Series(data=s.values, index=map(lambda x:datetime.datetime.fromtimestamp(x,tz=utc), s.index))
使用
来指定日期时间,您可以提供一个单位来选择整数的含义

In [50]: pd.to_datetime(s,unit='s')
Out[50]: 
0   1970-01-01 00:00:00
1   1970-01-01 00:00:01
2   1970-01-01 00:00:02
3   1970-01-01 00:00:03
4   1970-01-01 00:00:04
5   1970-01-01 00:00:05
6   1970-01-01 00:00:06
7   1970-01-01 00:00:07
8   1970-01-01 00:00:08
9   1970-01-01 00:00:09
dtype: datetime64[ns]

In [51]: pd.to_datetime(s,unit='ms')
Out[51]: 
0          1970-01-01 00:00:00
1   1970-01-01 00:00:00.001000
2   1970-01-01 00:00:00.002000
3   1970-01-01 00:00:00.003000
4   1970-01-01 00:00:00.004000
5   1970-01-01 00:00:00.005000
6   1970-01-01 00:00:00.006000
7   1970-01-01 00:00:00.007000
8   1970-01-01 00:00:00.008000
9   1970-01-01 00:00:00.009000
dtype: datetime64[ns]

In [52]: pd.to_datetime(s,unit='D')
Out[52]: 
0   1970-01-01
1   1970-01-02
2   1970-01-03
3   1970-01-04
4   1970-01-05
5   1970-01-06
6   1970-01-07
7   1970-01-08
8   1970-01-09
9   1970-01-10
dtype: datetime64[ns]
因此,创建一个系列很简单

In [54]: Series(s.values,index=pd.to_datetime(s,unit='s'))
Out[54]: 
1970-01-01 00:00:00    0
1970-01-01 00:00:01    1
1970-01-01 00:00:02    2
1970-01-01 00:00:03    3
1970-01-01 00:00:04    4
1970-01-01 00:00:05    5
1970-01-01 00:00:06    6
1970-01-01 00:00:07    7
1970-01-01 00:00:08    8
1970-01-01 00:00:09    9
dtype: int64
In [50]: pd.to_datetime(s,unit='s')
Out[50]: 
0   1970-01-01 00:00:00
1   1970-01-01 00:00:01
2   1970-01-01 00:00:02
3   1970-01-01 00:00:03
4   1970-01-01 00:00:04
5   1970-01-01 00:00:05
6   1970-01-01 00:00:06
7   1970-01-01 00:00:07
8   1970-01-01 00:00:08
9   1970-01-01 00:00:09
dtype: datetime64[ns]

In [51]: pd.to_datetime(s,unit='ms')
Out[51]: 
0          1970-01-01 00:00:00
1   1970-01-01 00:00:00.001000
2   1970-01-01 00:00:00.002000
3   1970-01-01 00:00:00.003000
4   1970-01-01 00:00:00.004000
5   1970-01-01 00:00:00.005000
6   1970-01-01 00:00:00.006000
7   1970-01-01 00:00:00.007000
8   1970-01-01 00:00:00.008000
9   1970-01-01 00:00:00.009000
dtype: datetime64[ns]

In [52]: pd.to_datetime(s,unit='D')
Out[52]: 
0   1970-01-01
1   1970-01-02
2   1970-01-03
3   1970-01-04
4   1970-01-05
5   1970-01-06
6   1970-01-07
7   1970-01-08
8   1970-01-09
9   1970-01-10
dtype: datetime64[ns]
In [54]: Series(s.values,index=pd.to_datetime(s,unit='s'))
Out[54]: 
1970-01-01 00:00:00    0
1970-01-01 00:00:01    1
1970-01-01 00:00:02    2
1970-01-01 00:00:03    3
1970-01-01 00:00:04    4
1970-01-01 00:00:05    5
1970-01-01 00:00:06    6
1970-01-01 00:00:07    7
1970-01-01 00:00:08    8
1970-01-01 00:00:09    9
dtype: int64