I've some csv files with the following format:
330913;23;2;2013;0;0;6;8;7
330914;23;2;2013;0;5;25;8;7
330915;23;2;2013;0;10;11;8;7
330916;23;2;2013;0;15;30;8;7
330917;23;2;2013;0;20;17;8;7
330918;23;2;2013;0;25;4;8;7
I read them into a pandas DataFrame and need to specify a column (say) 'dt'
with the date and time. My best try so far is the following:
df = pd.read_csv( './cucu.csv', sep=';', \
header=None, dtype='str' )
df[ 'dt' ] = pd.to_datetime(\
df[3]+df[2]+df[1]+df[4]+df[5]+df[6], \
format='%Y%m%d%H%M%S')
My question is, how do I do that without handling strings? I'm pretty sure I've done this in the past using something like:
df = pd.read_csv( './cucu.csv', sep=';', header=None, \
parse_dates={'dt': [3,2,1,4,5,6]} )
but it's not working right now: I get a column dt
with strings like 2013 2 23 0 0 6
What am I missing?
Check out the read_csv
method. Specifically, the date_parser
kwarg is what you are looking for. It takes the resulting string created by the parse_date
columns and processes it.
df = pd.read_csv('./cucu.csv', sep=';', header=None, parse_dates={'dt': [3,2,1,4,5,6]}, date_parser=lambda dts: pd.to_datetime(dts, format='%Y %m %d %H %M %S'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.