简体   繁体   English

将 Unix 纪元时间戳的 np.array 转换为具有正确时区的日期时间 [PYTHON, NUMPY]

[英]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)

转换 unix 时间戳的 np.array (dtype ' <u21') to np.datetime64< div><div id="text_translate"><p> 我正在寻找处理大量数据,因此我对计算以下内容的最快方法感兴趣:</p><p> 我将以下 np.array 作为 np.ndarray 的一部分,我想将其从“&lt;U21”转换为“np.datetime64”(毫秒)。</p><p> 当我在一个条目上执行以下代码时,它可以工作:</p><pre> tmp_array[:,0][0].astype(int).astype('datetime64[ms]')</pre><p> 结果:numpy.datetime64('2019-10-09T22:54:00.000')</p><p> 当我像这样在子数组上执行相同的操作时:</p><pre> tmp_array[:,0] = tmp_array[:,0].astype(int).astype('datetime64[ms]')</pre><p> 我总是收到以下错误:</p><pre> RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 21</pre><p> numpy 版本 1.22.4</p><pre> array(['1570661640000', '1570661700000', '1570661760000'],dtype='&lt;U21')</pre></div></u21')> - Converting np.array of unix timestamps (dtype '<U21') to np.datetime64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unix 纪元时间戳转换为 Python 中的 UTC 时区 - Unix epoch timestamps converted to UTC timezone in Python Python 基础:将 np.array 转换为 - Python Basics: Convert np.array to 转换 unix 时间戳的 np.array (dtype ' <u21') to np.datetime64< div><div id="text_translate"><p> 我正在寻找处理大量数据,因此我对计算以下内容的最快方法感兴趣:</p><p> 我将以下 np.array 作为 np.ndarray 的一部分,我想将其从“&lt;U21”转换为“np.datetime64”(毫秒)。</p><p> 当我在一个条目上执行以下代码时,它可以工作:</p><pre> tmp_array[:,0][0].astype(int).astype('datetime64[ms]')</pre><p> 结果:numpy.datetime64('2019-10-09T22:54:00.000')</p><p> 当我像这样在子数组上执行相同的操作时:</p><pre> tmp_array[:,0] = tmp_array[:,0].astype(int).astype('datetime64[ms]')</pre><p> 我总是收到以下错误:</p><pre> RuntimeError: The string provided for NumPy ISO datetime formatting was too short, with length 21</pre><p> numpy 版本 1.22.4</p><pre> array(['1570661640000', '1570661700000', '1570661760000'],dtype='&lt;U21')</pre></div></u21')> - Converting np.array of unix timestamps (dtype '<U21') to np.datetime64 在python中将np.ndarray转换为np.array - Convert np.ndarray to np.array in python 如何将 python np.array 转换为 cv2 图像 - How to convert python np.array to cv2 image Python PIL(Pillow):有效地将图像列表转换为np.array吗? - Python PIL(Pillow): Convert a list of images into np.array efficiently? Python 2.7将标准unix时间戳转换为纪元时间 - Python 2.7 Converting standard unix timestamps to epoch time Python np.array示例 - Python np.array example 如何使用 pandas 中的时区将 unix 纪元时间转换为日期时间 - How to convert unix epoch time to datetime with timezone in pandas Numpy np.array,带有dtype TypeError - Numpy np.array with dtype TypeError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM