2016-08-09 157 views
0

我有不同來源的數據列,因此與時間戳串稍微不一致問題:轉換「稍微不一致」熊貓列於日期時間

data_test  DataTime 
0 2012-10-03 12:14:18.257000000 
1 2012-10-01 08:39:54.633000000 
2 2012-10-05 07:50:14.203000000 
3 2012-10-02 15:02:42.843000000 
4    2012-10-02 09:02:13 
5    2012-10-02 09:02:13 
6    2012-10-09 11:00:36 
7    2012-10-09 11:00:36 

有些「第二的是整的,有些是浮點數,所以無論下面的方法會失敗:

import datetime as dt 
#Method 1: consider the float 
data_test['DataTime'] = data_test['DataTime'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S.%f')) 
#Method 2: ignore the float 
data_test['DataTime'] = data_test['DataTime'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S')) 

有沒有簡單的方法,我可以此列轉換爲日期時間?

回答

1

您可以使用to_datetime()方法:

In [222]: df 
Out[222]: 
         DataTime 
0 2012-10-03 12:14:18.257000000 
1 2012-10-01 08:39:54.633000000 
2 2012-10-05 07:50:14.203000000 
3 2012-10-02 15:02:42.843000000 
4   2012-10-02 09:02:13 
5   2012-10-02 09:02:13 
6   2012-10-09 11:00:36 
7   2012-10-09 11:00:36 

In [223]: df.dtypes 
Out[223]: 
DataTime object 
dtype: object 

In [224]: df.DataTime = pd.to_datetime(df.DataTime) 

In [225]: df 
Out[225]: 
       DataTime 
0 2012-10-03 12:14:18.257 
1 2012-10-01 08:39:54.633 
2 2012-10-05 07:50:14.203 
3 2012-10-02 15:02:42.843 
4 2012-10-02 09:02:13.000 
5 2012-10-02 09:02:13.000 
6 2012-10-09 11:00:36.000 
7 2012-10-09 11:00:36.000 

In [226]: df.dtypes 
Out[226]: 
DataTime datetime64[ns] 
dtype: object 
+0

它的工作原理!謝謝!熊貓比我想象的要聰明得多! ;) – Sidney

+0

@ user81640,是的,熊貓API真的很強大:) – MaxU