更改Python上熊猫的日期解析器上的默认日期
我有一个如下所示的数据集:更改Python上熊猫的日期解析器上的默认日期,python,date,pandas,time,Python,Date,Pandas,Time,我有一个如下所示的数据集: 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 ... 我想将始终为2012年2月8日的日期更改为2010年1月1日。我试着做到以下几点: impo
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
...
我想将始终为2012年2月8日的日期更改为2010年1月1日。我试着做到以下几点:
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))
但我有一个错误。简而言之,我想保持时间不变,改变日期。原因是我有多个数据集,每个数据集代表一个设备的日常电源配置文件。我不在乎日期,只在乎时间。我想阅读所有相同日期的文件,以便同步它们 您可以在
date\u解析器中首先尝试转换为,然后尝试替换:
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