簡體   English   中英

將 Unix 紀元時間戳的 np.array 轉換為具有正確時區的日期時間 [PYTHON, NUMPY]

[英]Convert np.array of Unix Epoch timestamps to Date Time with correct Timezone [PYTHON, NUMPY]

我有一組 Unix 紀元時間戳,我需要將其轉換為 python 中的日期時間格式。

我可以使用 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)

我遇到的問題是時區錯誤,我在紐約,所以需要將其設置為“EST”。

我嘗試通過在pd.to_datetime function 中設置utc=True來解決,但它仍然默認為“GMT”(提前 5 小時)。

我也嘗試了datetime.fromtimestamp(0)但它似乎只適用於單個元素而不是 arrays - https://note.nkmk.me/en/python-unix-time-datetime/

轉換紀元時是否有任何有效的方法來設置時區?

發現這可以通過 pandas 使用 .tz_* 方法來實現:

.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

工作代碼:

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 時區 Python 基礎:將 np.array 轉換為 轉換 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')> 在python中將np.ndarray轉換為np.array 如何將 python np.array 轉換為 cv2 圖像 Python PIL(Pillow):有效地將圖像列表轉換為np.array嗎? Python 2.7將標准unix時間戳轉換為紀元時間 Python np.array示例 如何使用 pandas 中的時區將 unix 紀元時間轉換為日期時間 Numpy np.array,帶有dtype TypeError
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM