Issue
I am attempting to convert a decimal number to a datatime. An example dateframe is:
timestep_time vehicle_angle vehicle_id vehicle_pos vehicle_speed vehicle_x vehicle_y Cluster
0 0.00 113.79 0 5.10 0.00 295.36 438.47 1
1 1.00 113.79 0 6.73 1.63 296.85 437.82 1
2 1.01 203.94 1 5.10 0.00 278.32 434.32 9
3 2.00 113.79 0 10.00 3.27 299.84 436.50 1
4 2.01 203.94 1 6.50 1.40 277.75 433.04 9
5 2.02 23.94 2 5.10 0.00 255.88 375.91 1
I am not super concerned with the actual units of time, I just need it as a datetime object. So, I have tried:
posBirch["timestep_time"] = pd.to_datetime(posBirch["timestep_time"], format='%M.%S')
and
posBirch["timestep_time"] = pd.to_datetime(posBirch["timestep_time"], format='%S.%f')
# Error:
ValueError: time data '0' does not match format '%M.%S' (match)
posBirch["timestep_time"] = pd.to_datetime(posBirch["timestep_time"], format='%S')
# Yields no error but drops decimal which I need to keep timesteps unique which I will later use for indexing i.e.:
1 1900-01-01 00:00:01 113.79 0 6.73 1.63 296.85 437.82 1
2 1900-01-01 00:00:01 203.94 1 5.10 0.00 278.32 434.32 9
posBirch["timestep_time"] = pd.to_datetime(posBirch["timestep_time"], format='%-S')
# Error:
ValueError: '-' is a bad directive in format '%-S'
How can I just convert this decimal to a unique datetime value? Units do not matter too much as this is a pseudo-timestep anyway, but I guess ideally seconds.
Solution
Here's what ended up working:
posBirch['TimeStamp'] = pd.to_datetime(posBirch['timestep_time'], unit='s')
Answered By - Sam Dean
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.