2017-08-02 48 views
2

我试图读取微秒格式的日期时间。阅读时间戳的python pandas

1500909283.955000 

预期的输出应该是这样的

July 24, 2017 3:14:43.955 PM 

但是,当我用熊猫我to_datetime功能

1970-01-01 00:00:01.500909283 

我尝试了所有可能的格式,但没有成功。

任何提示

回答

3

您需要to_datetimeunit='s' PARAM:

In[4]: 
pd.to_datetime(1500909283.955000, unit='s') 

Out[4]: Timestamp('2017-07-24 15:14:43.955000') 

时间戳秒以来的划时代

默认unit值是纳秒:

In[5]: 
pd.to_datetime(1500909283.955000, unit='ns') 

Out[5]: Timestamp('1970-01-01 00:00:01.500909283') 

这就是你观察

+0

感谢您的支持回复 – Sharek

2

需要to_datetime与参数unit,数据是seconds,而不是在milisecond S:

df = pd.DataFrame({'col':['1500909283.955000','1500909283.955000']}) 
df['col'] = pd.to_datetime(df['col'], unit='s') 
print (df) 
         col 
0 2017-07-24 15:14:43.955 
1 2017-07-24 15:14:43.955 

对于milliseconds

df['col'] = pd.to_datetime(df['col'], unit='ms') 
print (df) 
         col 
0 1970-01-18 08:55:09.283955 
1 1970-01-18 08:55:09.283955 

如果需要其他格式使用Series.dt.strftime

df['col1'] = df['col'].dt.strftime('%b %d, %Y %I:%M:%S.%f %p') 
print (df) 
         col        col1 
0 2017-07-24 15:14:43.955 Jul 24, 2017 03:14:43.955000 PM 
1 2017-07-24 15:14:43.955 Jul 24, 2017 03:14:43.955000 PM 
+2

微秒是''us''不是''ms'',''ms''是毫秒 – EdChum

+0

感谢您的回复 – Sharek

+0

@EdChum - 谢谢:) – jezrael

相关问题