Search code examples
pythonpython-2.7pandasdatetimeepoch

epoch nanoseconds to normal time


i have a data as

name        time
0   acn  1530677359000000000
1   acn  1530677363000000000 
2   acn  1530681023000000000 
3   acn  1530681053000000000 
4   acn  1530681531000000000 
5   acn  1530681561000000000

So I would like to change the time column to datetime format.

I tried doing this by executing:

df['time'] = pd.to_datetime(df['time'])

but it gives improper output as :

Out[70]:
0 2078-09-01 09:55:28.826007552
1 2078-10-17 17:02:08.826007552
2 2194-10-10 19:42:08.826007552
3 2195-09-23 01:02:08.826007552
4 2210-11-16 10:48:48.826007552
5 2211-10-29 16:08:48.826007552

I am on python 2.7. kindly take a look.


Solution

  • Vectorised, you can use pd.to_datetime with unit='ns'.

    df['datetime'] = pd.to_datetime(df['time'], unit='ns')
    
    print(df)
    
      name                 time            datetime
    0  acn  1530677359000000000 2018-07-04 04:09:19
    1  acn  1530677363000000000 2018-07-04 04:09:23
    2  acn  1530681023000000000 2018-07-04 05:10:23
    3  acn  1530681053000000000 2018-07-04 05:10:53
    4  acn  1530681531000000000 2018-07-04 05:18:51
    5  acn  1530681561000000000 2018-07-04 05:19:21