'Append nanosecond to millisecond Python datetime object

I am trying to append nanoseconds to an already existing millisecond datetime pandas object. So, for instance, I already have 08:02:36.715647 which reports uptil milliseconds (under column time_m). I then have a separate column titled "nano" that contains the nano part of the datetime (for instance, 976). How can I append this nanosecond part, so that the datetime reads as 08:02:36.715647976.

Thanks



Solution 1:[1]

you could add as a timedelta; EX:

import pandas as pd

ns = 976

t = "08:02:36.715647"

print(pd.to_datetime(t) + pd.to_timedelta(ns, unit='ns'))
2022-04-29 08:02:36.715647976

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FObersteiner