简体   繁体   中英

Convert dataframe with odd timestamp to timeseries with pandas

How would you convert the the following dataframe into a timeseries with pandas?

    Date                                Open    High    Low     Close
0   25/07/14 09 h 31 min 00 s +02:00    -1      -1      -887    -448
1   25/07/14 09 h 32 min 00 s +02:00    -425    -385    -455    -414
2   25/07/14 09 h 33 min 00 s +02:00    -432    -432    -654    -601

Starting with this:

>>> ts
0    25/07/14 09 h 31 min 00 s +02:00
1    25/07/14 09 h 32 min 00 s +02:00
2    25/07/14 09 h 33 min 00 s +02:00
dtype: object

you may specify the date-time format as below:

>>> pd.to_datetime(ts, format='%d/%m/%y %H h %M min %S s +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:

>>> _ + 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 are needed.

Alternatively, you may transform +02:00 to UTC offset format and parse it with '%z' directive:

>>> ts = ts.str.replace(r'(\d\d):(\d\d)$', r'\1\2')
>>> ts
0    25/07/14 09 h 31 min 00 s +0200
1    25/07/14 09 h 32 min 00 s +0200
2    25/07/14 09 h 33 min 00 s +0200
dtype: object
>>> ts.map(lambda t: dt.datetime.strptime(t, '%d/%m/%y %H h %M min %S s %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
dtype: object

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM