12

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???
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
ChromeHearts
  • 1,693
  • 1
  • 17
  • 27

3 Answers3

13

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

You can access it via its value:

import pandas as pd
ns = 1470924597871000000   
timestamp = pd.to_datetime(ns)
timestamp.value
Out: 1470924597871000000
ayhan
  • 70,170
  • 20
  • 182
  • 203
1

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
EdChum
  • 376,765
  • 198
  • 813
  • 562