我正在使用panda生成一个数据范围,在转换为epoch后,我意识到mktime
函数给我的是本地时区,而不是UTC。
如何获得UTC历元中的日期列表(毫秒(?
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)
将date_range转换为整数纳秒,除以1e6得到毫秒。
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')
现在你也可以转换为列表:
unixmilli.to_list()
[1643587200000.0,
1646006400000.0,
1648684800000.0,
1651276800000.0,
1653955200000.0,
1656547200000.0,
1659225600000.0,
1661904000000.0,
1664496000000.0]