在 pandas.Series 中将时间戳转换为 datetime.datetime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22554339/
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
convert timestamp to datetime.datetime in pandas.Series
提问by John
I have pandas Series where index is a list of integer (timestamp), how can I convert them to datetime.datetime (with timezone) more efficient than below raw conversion?
我有Pandas系列,其中 index 是一个整数列表(时间戳),如何将它们转换为 datetime.datetime(带时区)比低于原始转换更有效?
pd.Series(data=s.values, index=map(lambda x:datetime.datetime.fromtimestamp(x,tz=utc), s.index))
回答by Jeff
In [49]: s = Series(range(10))
Using to_datetime, you can supply a unit to select what the meaning of the integers.
使用to_datetime,您可以提供一个单位来选择整数的含义。
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]
Creating a Series is then straightforward
创建一个系列就很简单了
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
回答by unutbu
In [63]: s = pd.Series(range(10))
In [64]: s.index = pd.DatetimeIndex(s.index.asi8*10**9, tz='utc')
In [65]: s
Out[65]: 
1970-01-01 00:00:00+00:00    0
1970-01-01 00:00:01+00:00    1
1970-01-01 00:00:02+00:00    2
1970-01-01 00:00:03+00:00    3
1970-01-01 00:00:04+00:00    4
1970-01-01 00:00:05+00:00    5
1970-01-01 00:00:06+00:00    6
1970-01-01 00:00:07+00:00    7
1970-01-01 00:00:08+00:00    8
1970-01-01 00:00:09+00:00    9
dtype: int64

