Reputation: 475
My df looks like this. I tried many ways to convert the time of:
hour:minute:second microseconds
, but hit a wall. Below is the sample.df.
I need the Time and micro-seconds (the 3 numbers after 00:00:02 948
, so the micro seconds in this row would be: 948.
How to make another column of Time object that pandas will be able to work- like with fast vectorizing (not iterating) later on. I do not need year, month, day, date. Just need theTime, with microseconds, formatted in a way to be acceptable for work by pd, like making aggregated time intervals etc. Any ideas would be greatly appreciated!
idx, time, col2, col3, col4, col5, col6
0 00:00:02 948 41 14 34 36 55
1 00:00:03 228 44 -12 32 41 55
Upvotes: 1
Views: 1069
Reputation: 3684
You only have a "timedelta", not a datetime. So by converting it to datetime, pandas assumes time after 1900-01-01, because thats the definition (timestamp of 0).
I did not completely understand how your output is supposed to look like, so feel free to ask how to change it if I misunderstood.
You can access attributes for minute/second/microsecond independently and format them like this:
import pandas as pd
df = pd.DataFrame(columns=["time"], data=["00:00:02 948", "00:00:03 228"])
df["time"] = pd.to_datetime(df["time"], format='%H:%M:%S %f').apply(lambda dt: f"{dt.minute:02d}:{dt.second:02d}.{dt.microsecond:06d}")
outputs:
time
0 00:02.948000
1 00:03.228000
If you only want to get seconds and microseconds, just remove dt.minute
from the formatting.
pd.to_datetime(df["time"], format='%H:%M:%S %f').apply(lambda dt: f"{dt.second:02d}.{dt.microsecond:06d}")
time
0 02.948000
1 03.228000
Upvotes: 1