python - Convert dataframe with odd timestamp to timeseries with pandas -
How can you change the following dataframe in series with Panda?
Open Date High Low Close 0 25/07/14 09 Hours 31 Minutes 00 to +02: 00 -1 -1887 -448 1 25/07/14 09 Hours 32 Minutes 00 From +02: 00 -425 -385 -455 - 414 2 25/07/14 09 hours 33 minutes 00 to +02: 00 -432 -432 -654 -601
Start with:
& gt; & Gt; & Gt; Ts 0 25/07/14 09 hours 31 minutes 00 to + 02: 00 1 25/07/14 09 hours 32 mins 00 to +02: 00 2 25/07/14 09 hours 33 mins 00 to +2: 00 dtp : Object
You can specify the date-time format according to:
& gt; & Gt; & Gt; Pd.to_datetime (ts, format = '% d /% m /% y% hh% m min% ss +02: 00') 0 2014-07-25 09:31:00 1 2014-07-25 09: 32 : 00 2 2014-07-25 09:33:00 dtype: datetime64 [ns]
If +2: 00
means 2 hours offset, then :
& gt; & Gt; & Gt; _ + Np.timedelta64 (2, 'h') 0 2014-07-25 11:31:00 1 2014-07-25 11:32:00 2 2014-07-25 11:33:00 dtype: datetime64 [ns ]
Of course, if + 02: 00
has a different meaning or is not fixed, then more string manipulation is required.
Alternatively, you can convert +02: 00
to UTC offset format and parse it with the '% z' directive:
gt; & Gt; ; & Gt; Ts = ts.str.replace (r '(\ d \ d): (\ d \ d) $', r '\ 1 \ 2')> gt; & Gt; & Gt; Ts 0 25/07/14 09 hrs 31 min 00 to +2002 25/07/14 09 hours 32 minutes 00 to +2002 02/07/14 09 hours 33 minutes 00 to +0200 DTP: Object & gt; & Gt; & Gt; Ts.map (Lambda T: dt.datetime.strptime (t, '% d /% m /% y% hh% m min% ss% z')) 0 2014-07-25 09: 31: 00+ 02: 00 1 2014-07-25 09: 32: 00 + 02: 00 2 2014-07-25 09: 33: 00 + 02: 00 DTP: Object
Comments
Post a Comment