0

I found a few approaches how to convert the Pandas datetime to Unix timestamp, however the results are not the same. What am I doing wrong, why the res1 = res2 = res3 != res4? (there is 3,600 second difference). Thank you!

res1 = Float64Index([1,677,628,800.0], dtype='float64')

res2 = Float64Index([1,677,628,800.0], dtype='float64')

res3 = 1,677,628,800

res4 = 1,677,625,200

From https://www.unixtimestamp.com/, the correct result should be 1677625200.

import pandas as pd
import time

# https://stackoverflow.com/questions/54313463/pandas-datetime-to-unix-timestamp-seconds
print('res1 = ' + str(pd.to_datetime(['2023-03-01 00:00:00'], utc=True).astype('int64') / 10**9))


print('res2 = ' + str((pd.to_datetime(['2023-03-01 00:00:00 00:00']) - pd.Timestamp("1970-01-01")).total_seconds()))


print('res3 = ' + str(pd.Timestamp('2023-03-01 00:00:00 00:00').timestamp()))


# https://www.geeksforgeeks.org/how-to-convert-datetime-to-unix-timestamp-in-python/
print('res4 = ' + str(time.mktime(pd.to_datetime(['2023-03-01 00:00'], utc=True)[0].timetuple())))
Progman
  • 16,827
  • 6
  • 33
  • 48
  • Well, if I enter 1677628800 in https://www.unixtimestamp.com/, I get Wed Mar 01 2023 00:00:00 GMT+0000. I also get 1677628800 if I enter this on the command-line in Linux 'date --date "2023-03-01 00:00:00+00:00" "+%s" '. So I guess 1677628800 is the right answer. – EvensF May 14 '23 at 17:13

0 Answers0