转换GMT和当地时间不匹配

2024-03-29 12:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将UTC数据转换为莫桑比克当地时间。莫桑比克当地时间为格林尼治标准时间+2或非洲/马普托。但是,当使用.tz_localize('UTC').tz_convert(X)时,其中X可以是= 'GMT+2'= 'Africa/Maputo',我会得到不同的答案。例如:

import pandas as pd 
import numpy as np

np.random.seed(2019)
N = 1000
rng = pd.date_range('2019-01-01', freq='10Min', periods=N)
df = pd.DataFrame(np.random.rand(N, 3), columns=['temp','depth','acceleration'], index=rng)

print(df.tz_localize('UTC').tz_convert('Etc/GMT+2'))

print(df.tz_localize('UTC').tz_convert('Africa/Maputo'))

解决我的问题的代码是:df.tz_localize('UTC').tz_convert('Africa/Maputo')。因此,我想知道我是否误解了tz_convert('Etc/GMT+2')方法,以及为什么两种不同的解决方案不能提供相同的答案tz_convert('Etc/GMT-2')解决了这个问题,但至少对我来说,这不是直觉

提前谢谢


1条回答
网友
1楼 · 发布于 2024-03-29 12:23:35

使用etcetera进行时区转换的工作原理正好相反,考虑到对其documentation的以下观察,也许应该完全反对这种转换:

These entries are mostly present for historical reasons, so that people in areas not otherwise covered by the tz files could "zic -l" to a time zone that was right for their area. These days, the tz files cover almost all the inhabited world, so there's little need now for the entries that are not on UTC.

您的解决方法是正确的,您可以在here找到最佳解释。也许还是用tz_convert('Africa/Maputo')

相关问题 更多 >