2

I have a DataFrame with one column sensorTime representing timestamps in nanoseconds (currentTimeMillis() * 1000000). An example is the following:

sensorTime
1597199687312000000
1597199687315434496
1597199687320437760
1597199687325465856
1597199687330448640
1597199687335456512
1597199687340429824
1597199687345459456
1597199687350439168
1597199687355445504

I'm transforming this column to datetime using

res['sensorTime'] = pd.to_datetime(res['sensorTime'])

How can I transform the datetime back to timestamps in nanoseconds so that I get the exact values as in the example above?

machinery
  • 5,972
  • 12
  • 67
  • 118
  • 2
    Does this answer your question? [Convert pandas DateTimeIndex to Unix Time?](https://stackoverflow.com/questions/15203623/convert-pandas-datetimeindex-to-unix-time) – FObersteiner Jan 26 '21 at 20:44
  • @MrFuppes your second link worded. It is `res['sensorTime'] = res['sensorTime'].astype(np.int64)`. Please submit an answer so that I can accept it. – machinery Jan 26 '21 at 23:01

1 Answers1

1

The obvious way to do this would be to convert the datetime series to Unix time (nanoseconds since the epoch) using astype:

import pandas as pd
res = pd.DataFrame({'sensorTime': [1597199687312000000, 1597199687315434496, 1597199687320437760]})
res['sensorTime'] = pd.to_datetime(res['sensorTime'])

# back to nanoseconds / Unix time - .astype to get a pandas.series
s = res['sensorTime'].astype('int64')

print(type(s), s)
<class 'pandas.core.series.Series'> 
0    1597199687312000000
1    1597199687315434496
2    1597199687320437760
Name: sensorTime, dtype: int64

Another option is to use a view:

# .view to get a numpy.ndarray 
arr = res['sensorTime'].values.view('int64')

print(type(arr), arr)
<class 'numpy.ndarray'> 
[1597199687312000000 1597199687315434496 1597199687320437760]

But be careful, it's just a view, so the underlying data stays the same. Meaning that here, if you change a value in the dataframe's Series, this change will also be visible in the view array.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72