无法更改pandas中的日期时间

时间:2016-04-09 09:15:30

标签: python datetime pandas dataframe

我有以下csv数据:

sep=;
ACCELEROMETER X (m/s²);ACCELEROMETER Y (m/s²);ACCELEROMETER Z (m/s²);GRAVITY X (m/s²);GRAVITY Y (m/s²);GRAVITY Z (m/s²);LINEAR ACCELERATION X (m/s²);LINEAR ACCELERATION Y (m/s²);LINEAR ACCELERATION Z (m/s²);GYROSCOPE X (rad/s);GYROSCOPE Y (rad/s);GYROSCOPE Z (rad/s);LIGHT (lux);MAGNETIC FIELD X (μT);MAGNETIC FIELD Y (μT);MAGNETIC FIELD Z (μT);ORIENTATION Z (azimuth °);ORIENTATION X (pitch °);ORIENTATION Y (roll °);PROXIMITY (m);ATMOSPHERIC PRESSURE (hPa);TEMPERATURE (C);RELATIVE HUMIDITY (%);SOUND LEVEL (dB);LOCATION Latitude : ;LOCATION Longitude : ;LOCATION Altitude ( m);LOCATION Altitude-google ( m);LOCATION Altitude-atmospheric pressure ( m);LOCATION Speed ( m/s);LOCATION Accuracy ( m);LOCATION ORIENTATION (°);Satellites in range;Time since start in ms ;YYYY-MO-DD HH-MI-SS_SSS
0.2825;0.3932;10.0299;-0.219;2.4781;9.4859;0.5052;-2.0443;0.5388;0.0699;0.0215;0.0045;18.0;43.3;-52.23;64.99;213.54;-15.07;1.18;8.0;1023.4;23.0;63.3;61.013;32.06071;34.775364;;29.3243923;;;30.0;;0 / 0;6;2015-12-07 20:51:06:608
0.6422;-0.5429;10.076;-0.023;1.4988;9.6914;0.7552;-1.3062;-0.0186;0.0046;0.0582;0.1172;19.0;43.6;-51.87;70.25;215.0;-9.66;-0.18;8.0;1023.37;23.0;63.3;41.656;32.06071;34.775364;;29.3243923;;;30.0;;0 / 0;1007;2015-12-07 20:51:07:609

当我尝试使用命令时:

data['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(data['YYYY-MO-DD HH-MI-SS_SSS'], format='%Y-%m-%d %H:%M:%S:%f')

我收到以下错误:

  

KeyError:'YYYY-MO-DD HH-MI-SS_SSS'

有什么问题? (我是Python的新手,但我无法弄明白)

1 个答案:

答案 0 :(得分:1)

我认为您可以将参数skiprows=1添加到read_csv以跳过csv的第一行:

df = pd.read_csv('university.csv', sep=";", skiprows=1)

然后你可以转换to_datetime

df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'], 
                                               format='%Y-%m-%d %H:%M:%S:%f')

或者您可以将read_csv与参数parse_datesdate_parser一起使用:

import pandas as pd

dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S:%f')
df = pd.read_csv('university.csv', 
                 sep=";", 
                 skiprows=1, 
                 parse_dates=['YYYY-MO-DD HH-MI-SS_SSS'],
                 date_parser=dateparse)

print df.head()