我试图解析CSV文件的时间戳(第一列名为“时间”)。时间戳的格式为:01.10.2016 00:10:00
(dd.mm.yyyy HH:MM:SS
)
timestamp_parser = lambda x: pd.datetime.strptime(x, "%d.%m.%Y %H:%M:%S")
df_pi_data = pd.read_csv( "pi_daten.csv", usecols = (0,1), sep =';', thousands='.', decimal=',', names = ['time','temperature'], parse_dates=['time'], date_parser = timestamp_parser)
发生以下错误:
ValueError: time data '\xef\xbb\xbftime' does not match format '%d.%m.%Y %H:%M:%S'
@kantal:
time;temperature;
01.10.2016 00:00; 23,13854599;
01.10.2016 00:10; 23,24945831;
01.10.2016 00:20; 23,16853714;
答案 0 :(得分:0)
time;temperature
01.10.2016 00:00; 23,13854599
01.10.2016 00:10; 23,24945831
01.10.2016 00:20; 23,16853714
import pandas as pd
timestamp_parser = lambda x: pd.datetime.strptime(x, "%d.%m.%Y %H:%M")
df = pd.read_csv("test.txt", sep=";", decimal=',', \
parse_dates=['time'], date_parser = timestamp_parser)
我使用这些数据,并使用代码,成功工作
time temperature
0 2016-10-01 00:00:00 23.138546
1 2016-10-01 00:10:00 23.249458
2 2016-10-01 00:20:00 23.168537
答案 1 :(得分:0)
数据中没有“秒”,因此从“ timestamp_parser”中删除“%S”。 并在下面的pd.read_csv中查看修改后的args:
pd.read_csv("pi_daten.csv" , usecols = (0,1), \
sep=r'\s*;\s*', engine="python", \
thousands='.', decimal=',', \
parse_dates=['time'], date_parser = timestamp_parser)
编辑: 解决“十进制”分隔符问题的方法:
df["temperature"]= df.temperature.transform(lambda col: col.str.replace(',','.').astype(float))