Search code examples
pythonpandastimepython-fractions

table time conversion nanoseconds to minute


the first table to be converted to minute:

Time_sec >>> Time_Min)
362.7313    00:06:02,7
490.669     00:08:10,7
824.5673    00:13:44,6
951.829     00:15:51,8
13.0447     00:00:13,0
13.0678     00:00:13,1
122.5468    00:02:02,5
536.7552    00:08:56,8
811.9501    00:13:32,0
938.1133    00:15:38,1
13809718    00:02:19,0
619.4331    00:10:19,4
1407.5125   00:23:27,5
16290049    00:27:09,0

did try like this:

df['Time_sec'] = pd.to_datetime(df['Time_sec'].datetime.timedelta(seconds =df['time_sec'])

but for 1629,049 i get 16:29 what must be 27min an 9sec


Solution

  • A first solution would be to use the apply function to all rows :

    df["Time_Min"] = df["Time_Sec"].apply(lambda x: f"{x // 60}:{x % 60}")
    

    A second solution (maybe more optimized regarding pandas C implementation) :

    df["Time_Min"] = df["Time_Sec"].floordiv(60).map(str) + ":" + df["Time_Sec"].mod(60).map(str)