Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Pandas Timestamp into nano since Epoch?

What is the most efficient way to convert Pandas Timestamp into nano since Epoch?

import pandas as pd
ns = 1470924597871000000   
timestamp = pd.to_datetime(ns, unit="ns")

Then how to

timestamp => 1470924597871000000  ns???
like image 820
ChromeHearts Avatar asked Aug 11 '16 14:08

ChromeHearts


3 Answers

For me it works nice with parameter unit but surprisingly without parameter too:

import pandas as pd
ns = 1470924597871000000  

timestamp1 = pd.to_datetime(ns)
print (timestamp1)
2016-08-11 14:09:57.871000

timestamp = pd.to_datetime(ns, unit='ns')
print (timestamp)
2016-08-11 14:09:57.871000

And if need convert from Timestamp to epoch:

print (timestamp.value)
1470924597871000000
like image 155
jezrael Avatar answered Oct 06 '22 01:10

jezrael


You can access it via its value:

import pandas as pd
ns = 1470924597871000000   
timestamp = pd.to_datetime(ns)
timestamp.value
Out: 1470924597871000000
like image 29
ayhan Avatar answered Oct 06 '22 01:10

ayhan


pass unit='ns' to specify the unit type:

In [46]:
ns = 1470924597871000000   
timestamp = pd.to_datetime(ns, unit='ns')
timestamp

Out[46]:
Timestamp('2016-08-11 14:09:57.871000')

but this seems to work fine anyway without passing this unless you are after something else?

You can use timestamp() to get the timestamp value:

In [50]:
timestamp.timestamp()

Out[50]:
1470920997.871
like image 44
EdChum Avatar answered Oct 06 '22 00:10

EdChum