读取excel并将索引转换为datatimeindexpandas
我读了这样一个pandas的excel
df = pd.read_excel("Test.xlsx", index_col=[0])
数据框的外观与包含date和时间以及一列的索引类似:
01.01.2015 00:15:00 47.2 01.01.2015 00:30:00 46.6 01.01.2015 00:45:00 19.4 01.01.2015 01:00:00 14.8 01.01.2015 01:15:00 14.8 01.01.2015 01:30:00 16.4 01.01.2015 01:45:00 16.2 ...
我想将索引转换为datatimeindex,我试过了
df.index = pd.to_datetime(df.index)
并得到:“ValueError:未知的string格式”
这里最好的方法是将索引转换为包含date和tiem的数据时间格式,以使用基于datetime的函数
我想你需要添加参数format
– 请参阅http://strftime.org/ :
df.index = pd.to_datetime(df.index, format='%d.%m.%Y %H:%M:%S') print (df) a 2015-01-01 00:15:00 47.2 2015-01-01 00:30:00 46.6 2015-01-01 00:45:00 19.4 2015-01-01 01:00:00 14.8 2015-01-01 01:15:00 14.8 2015-01-01 01:30:00 16.4 2015-01-01 01:45:00 16.2