我读过这样的 Pandas excel
df = pd.read_excel("Test.xlsx", index_col=[0])
数据框看起来像这样,索引包含日期和时间以及一列:
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
...
我想将索引转换为数据时间索引,我试过了
df.index = pd.to_datetime(df.index)
并得到:“ValueError:未知的字符串格式”
将索引转换为包含日期和时间的数据时间格式以使用基于日期时间的函数的最佳方法是什么
最佳答案
我认为您需要添加参数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
关于python - 读取 excel 并将索引转换为 datetimeindex Pandas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40174721/