首页 > 解决方案 > 解析导致 ValueError 的日期时间

问题描述

我试图解析 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;

标签: pythonpandasparsing

解决方案


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

推荐阅读