【发布时间】:2014-05-19 21:48:20
【问题描述】:
我需要将时区感知 date_range (TimeStamps) 转换为 UNIX 纪元值,以便在外部 Javascript 库中使用。
我的做法是:
# Create localized test data for one day
rng = pd.date_range('1.1.2014', freq='H', periods=24, tz="Europe/Berlin")
val = np.random.randn(24)
df = pd.DataFrame(data=val, index=rng, columns=['values'])
# Reset index as df column
df = df.reset_index()
# Convert the index column to the desired UNIX epoch format
df['index'] = df['index'].apply(lambda x: x.value // 10**6 )
df['index'] 包含预期的 UNIX 纪元值,但它们存储在 UTC(!) 中。
我想这是因为 pandas 将时间戳存储在 numpy UTC datetime64 值中。
有没有一种聪明的方法可以在请求的时区中获取“正确”的纪元值?
【问题讨论】:
标签: python numpy pandas epoch datetime64