我的数据框中的一列是dtype datetime64
格式为2011-01-01 00:00:00
'%Y-%m-%dT%H:%M:%S'
现在,我想在新的df中复制该列,并将值转换为UTC时间戳。我尝试了以下方法:
from pytz import utc, timezone
from datetime import datetime
from time import mktime
input_dt = pd.to_datetime(df["BaseDateTime"])
input_dt
mktime(timezone('US/Eastern').localize(input_dt).utctimetuple())
#OR
mktime(utc.localize(input_dt).utctimetuple())
执行最后两个语句后,会抛出以下错误:
AttributeError: 'Series' object has no attribute 'tzinfo'
我应该使用哪种方法来获取数据帧的BaseDateTime col的utc时间戳?
答案 0 :(得分:0)
将datetime
值转换为纪元值的一种好方法是通过从要转换的日期减去纪元时间来创建datetime.timedelta
。此功能可以执行此操作,可以应用于pandas.Series
或pandas.DataFrame
的列。
<强>代码:强>
import pandas as pd
import datetime as dt
from pytz import timezone
def convert_naive_dt_to_utc_epoch(naive_dt, tz_info):
# assign proper timezone to datetime
aware = tz_info.localize(naive_dt).astimezone(timezone('UTC'))
# get a datetime that is equal to epoch in UTC
utc_at_epoch = timezone('UTC').localize(dt.datetime(1970, 1, 1))
# return the number of seconds since epoch
return (aware - utc_at_epoch).total_seconds()
测试代码:
data = [np.datetime64(x) for x in
"2016-10-18T13:44:59 2016-02-18T13:59:59".split()]
series = pd.Series(data=data, name='Date')
# apply the conversion function to series to create epoch series
epoch_series = series.apply(
lambda x: convert_naive_dt_to_utc_epoch(x, timezone('US/Eastern')))
print(epoch_series)
<强>结果:强>
0 1.476813e+09
1 1.455822e+09
Name: Date, dtype: float64