[英]Convert np.array of Unix Epoch timestamps to Date Time with correct Timezone [PYTHON, NUMPY]
I have an array of Unix Epoch time stampts that I need to convert to datetime format in python.我有一组 Unix 纪元时间戳,我需要将其转换为 python 中的日期时间格式。
I can make the conversion ok using numpy and pandas, as below:我可以使用 numpy 和 pandas 进行转换,如下所示:
tim = [1627599600,1627599600,1627599601,1627649998,1627649998,1627649999]
tim_np = np.array(tim)
tim_np = np.asarray(tim, dtype='datetime64[s]',)
tim_pd = pd.to_datetime(tim,unit='s', utc=True,)
print(tim_np)
print(tim_pd)
The problem I am running into is that the time zone is wrong, I am in NY so require it set to "EST".我遇到的问题是时区错误,我在纽约,所以需要将其设置为“EST”。
I tried addressing by setting utc=True
in the pd.to_datetime
function but it still keeps defaulting to "GMT" ( 5 hours ahead).我尝试通过在
pd.to_datetime
function 中设置utc=True
来解决,但它仍然默认为“GMT”(提前 5 小时)。
I also tried the datetime.fromtimestamp(0)
but it seemingly only works on single elements and not arrays - https://note.nkmk.me/en/python-unix-time-datetime/我也尝试了
datetime.fromtimestamp(0)
但它似乎只适用于单个元素而不是 arrays - https://note.nkmk.me/en/python-unix-time-datetime/
Is there any efficient method to set the time zone when converting epochs?转换纪元时是否有任何有效的方法来设置时区?
Found that this can be achieved with pandas using the.tz_* methods:发现这可以通过 pandas 使用 .tz_* 方法来实现:
.tz_localie - https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.tz_localize.html .tz_localie - https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.tz_localize.html
.tz_convert - https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.tz_convert.html .tz_convert - https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.tz_convert.html
Working code:工作代码:
tim = [1627599600,1627599600,1627599601,1627649998,1627649998,1627649999]
tim_pd = (pd.to_datetime(tim,unit='s')
.tz_localize('utc')
.tz_convert('America/New_York'))
print(tim_pd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.