Home > Mobile >  Pandas data_range to epoch in miliseconds
Pandas data_range to epoch in miliseconds

Time:10-16

I am generating a data range using pandas and after transforming to epoch, however I realized that the mktime function is given me my local timezone and not UTC.

How do I get an list of dates in UTC epoch (milliseconds)?

dates = pd.date_range(start='1/1/2022', end='10/1/2022',
                      freq='M', tz='UTC').strftime("%Y-%m-%dT%H:%M:%S.%fZ").tolist()

print(dates)

list_epoch=[]

for i in dates:
    epoch = int(time.mktime(time.strptime(i, "%Y-%m-%dT%H:%M:%S.%fZ")))*1000
    list_epoch.append(epoch)

print(list_epoch)

CodePudding user response:

convert the date_range to integer nanoseconds and divide by 1e6 to get milliseconds.

import pandas as pd

dates = pd.date_range(start='1/1/2022', end='10/1/2022',
                      freq='M', tz='UTC') # do not use strftime here

unixmilli = dates.astype(int) / 1e6
unixmilli
Float64Index([1643587200000.0, 1646006400000.0, 1648684800000.0,
              1651276800000.0, 1653955200000.0, 1656547200000.0,
              1659225600000.0, 1661904000000.0, 1664496000000.0],
             dtype='float64')

now you could also convert to list:

unixmilli.to_list()
[1643587200000.0,
 1646006400000.0,
 1648684800000.0,
 1651276800000.0,
 1653955200000.0,
 1656547200000.0,
 1659225600000.0,
 1661904000000.0,
 1664496000000.0]
  • Related