2016-03-02 56 views
1

我有一個數據集是這樣的:更改默認的日期上的Python

refrigerator.csv 
08/02/2012 00:00:02;136;134 
08/02/2012 00:00:03;134;134 
08/02/2012 00:00:05;136;134 
08/02/2012 00:00:06;136;134 
08/02/2012 00:00:08;134;134 
08/02/2012 00:00:09;134;134 
... 

我想改變,始終是08月02日至01/01/2010的日期。我試圖做到以下幾點:

import pandas as pd 
refr=pd.read_csv('C:/refrigerator.csv', names=['ts', 'P1', 'P2'], 
        sep=';', parse_dates=[0], index_col=0, 
        date_parser=lambda x: pd.Timestamp('2010-01-01 %s' %x)) 

但我得到一個ValueError。總之,我想保持現狀,並改變日期。原因是因爲我有多個數據集,每個數據集都代表設備的每日功率曲線。我不在乎日期,只有時間。我想用相同的日期閱讀所有這些文件以便同步它們。

回答

2

你可以嘗試先轉換爲to_datetime,然後在date_parserreplace

import pandas as pd 
import io 

temp=u""" 
08/02/2012 00:00:02;136;134 
08/02/2012 00:00:03;134;134 
08/02/2012 00:00:05;136;134 
08/02/2012 00:00:06;136;134 
08/02/2012 00:00:08;134;134 
08/02/2012 00:00:09;134;134""" 


#after testing replace io.StringIO(temp) to filename 
df =pd.read_csv(io.StringIO(temp), names=['ts', 'P1', 'P2'], 
        sep=';', parse_dates=[0], index_col=0, 
        date_parser=lambda x: pd.to_datetime(x).replace(year=2010, month=1,day=1)) 

print df 
         P1 P2 
ts       
2010-01-01 00:00:02 136 134 
2010-01-01 00:00:03 134 134 
2010-01-01 00:00:05 136 134 
2010-01-01 00:00:06 136 134 
2010-01-01 00:00:08 134 134 
2010-01-01 00:00:09 134 134 
+0

非常感謝!它非常完美! – user3423639