2014-04-02 70 views
2

我得到了一些數據,看起來像這樣:Python讀物蜱數據大熊貓

Symbol    Timestamp  bid  ask 
0 EUR/USD 20140101 21:55:34.378 1.37622 1.37693 
1 EUR/USD 20140101 21:55:40.410 1.37624 1.37698 
2 EUR/USD 20140101 21:55:47.210 1.37619 1.37696 
3 EUR/USD 20140101 21:55:57.963 1.37616 1.37696 
4 EUR/USD 20140101 21:56:03.117 1.37616 1.37694 

我試圖將其轉換爲大熊貓,但我堅持在微秒部分....下面就是我我有那麼遠,

讀取數據

sec = pd.read_csv('EURUSD.csv',header=None,parse_dates = 1) 
sec.columns = ['Symbol', 'Timestamp', 'bid', 'ask'] 

得到timestamp列轉換

sec['Timestamp'] = sec['Timestamp'].apply(lambda x: dt.datetime.strptime(x, '%Y%m%d %H%M%S')) 

'%Y%m%d %H%M%S'在上面的命令中,%S之後的部分應該是wad?它應該是'%Y%m%d %H%M%S%f'?但這似乎並不奏效。那段時間會攪亂一切......有什麼想法?

回答

1

只是適用to_datetime

In [11]: df 
Out[11]: 
    Symbol    Timestamp  bid  ask 
0 EUR/USD 20140101 21:55:34.378 1.37622 1.37693 
1 EUR/USD 20140101 21:55:40.410 1.37624 1.37698 
2 EUR/USD 20140101 21:55:47.210 1.37619 1.37696 
3 EUR/USD 20140101 21:55:57.963 1.37616 1.37696 
4 EUR/USD 20140101 21:56:03.117 1.37616 1.37694 

In [12]: pd.to_datetime(df.Timestamp) 
Out[12]: 
0 2014-01-01 21:55:34.378000 
1 2014-01-01 21:55:40.410000 
2 2014-01-01 21:55:47.210000 
3 2014-01-01 21:55:57.963000 
4 2014-01-01 21:56:03.117000 
Name: Timestamp, dtype: datetime64[ns] 

如果你必須通過格式,失落的一角是%f

In [12]: df['Timestamp'].apply(lambda x: dt.datetime.strptime(x, '%Y%m%d %H:%M:%S.%f')) 
Out[12]: 
0 2014-01-01 21:55:34.378000 
1 2014-01-01 21:55:40.410000 
2 2014-01-01 21:55:47.210000 
3 2014-01-01 21:55:57.963000 
4 2014-01-01 21:56:03.117000 
Name: Timestamp, dtype: datetime64[ns] 
+2

你可以直接通過格式轉換成to_datetime,無需申請 – Jeff