ValientProcess
ValientProcess

Reputation: 1801

Cannot change datetime in pandas

I have the following csv data:

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

and when i'm trying to use the command:

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')

I'm getting the following error:

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

What is wrong? (I'm new to Python, but I can't figure it out)

Upvotes: 1

Views: 190

Answers (1)

jezrael
jezrael

Reputation: 862406

I think you can add parameter skiprows=1 to read_csv for skipping first line of csv:

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

Then you can convert 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')

Or you can use read_csv with parameters parse_dates and date_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()  

Upvotes: 1

Related Questions