pandas 熊猫:时间戳到日期时间

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

Pandas: timestamp to datetime

pythondatetimepandas

提问by Petr Petrov

I have dataframe and column with dates looks like

我有数据框和日期列看起来像

 date
 1476329529    
 1476329530    
 1476329803 
 1476329805 
 1476329805  
 1476329805 

I use df['date'] = pd.to_datetime(df.date, format='%Y-%m-%d %H:%M:%S')to convert that, but I'm get strange result

df['date'] = pd.to_datetime(df.date, format='%Y-%m-%d %H:%M:%S')用来转换它,但我得到了奇怪的结果

 date 
 1970-01-01 00:00:01.476329529   
 1970-01-01 00:00:01.476329530   
 1970-01-01 00:00:01.476329803  
 1970-01-01 00:00:01.476329805    
 1970-01-01 00:00:01.476329805   
 1970-01-01 00:00:01.476329805   

Maybe I did anything wrong

也许我做错了什么

回答by EdChum

This looks look epoch timestamps which is number of seconds since 1st January 1970 :

这看起来像纪元时间戳,它是自 1970 年 1 月 1 日以来的秒数:

In [71]:
pd.to_datetime(df['date'], unit='s')
?
Out[71]:
0   2016-10-13 03:32:09
1   2016-10-13 03:32:10
2   2016-10-13 03:36:43
3   2016-10-13 03:36:45
4   2016-10-13 03:36:45
5   2016-10-13 03:36:45
Name: date, dtype: datetime64[ns]

回答by milos.ai

Something like this maybe:

可能是这样的:

import datetime
date = datetime.datetime.fromtimestamp(1476329529)
date
# gives
# datetime.datetime(2016, 10, 13, 5, 32, 9)

str(date) # '2016-10-13 05:32:09'