簡體   English   中英

根據數據幀的列將具有 UTC 時區的日期時間列轉換為另一個時區

[英]Converting a datetime column with UTC timezone to another TimeZone based on a column of the Dataframe

我有一個 Pandas 日期框,它有兩列,列名分別是“DateTimeInUTC”和“TimeZone”。 'DateTimeInUTC' 是 UTC 中實例的日期和時間,'TimeZone' 是實例所在位置的時區。

數據框中的一個實例可能是這樣的:

DateTimeInUTC: '2019-12-31 07:00:00'
TimeZone: 'US/Eastern'

我想使用 dType: datetime64 向數據幀添加另一列,該列將 'DateTimeInUTC' 轉換為該實例中的指定時區。


我嘗試使用Pandas.tz_convert()方法,但它將時區作為參數而不是數據Pandas.tz_convert()另一列


編輯:到目前為止,我最好的解決方案是使用 Pandas select 語句按時區拆分數據幀,然后將時區應用於具有相同時區的每個數據幀,然后連接所有數據幀

更好的解決方案:

我能夠大大改進我自己的解決方案:

timezones = weatherDf['TimeZone'].unique()
for timezone in timezones:
    weatherDf.loc[weatherDf['TimeZone'] == timezone, 'DateTimeInTimeZone'] = weatherDf.loc[weatherDf['TimeZone'] == timezone, 'DateTimeInUTC'].dt.tz_localize('UTC').dt.tz_convert(timezone).dt.tz_localize(None)

此解決方案在 3.6 秒內在我的系統上轉換了大約 700 萬個實例


我以前的解決方案:

此解決方案有效,但可能不是最佳選擇:

讓我們假設 weatherDf 是我的數據框,其中包含以下列: DateTimeInUTCTimeZone

timezones = weatherDf['TimeZone'].unique()
weatherDfs = []
for timezone in timezones:
    tempDf = weatherDf[weatherDf['TimeZone'] == timezone]
    tempDf['DateTimeInTimeZone'] = tempDf['DateTimeInUTC'].dt.tz_convert(timezone)
    weatherDfs.append(tempDf)
weatherDfConverted = pd.concat(weatherDfs)

這個解決方案在大約 40 秒內轉換了我系統上大約 700 萬個實例

使用groupby()

import pytz
import random
import time

tic = time.perf_counter()
ltz = len(pytz.all_timezones) - 1
length = 7 * 10 ** 6
pd.options.display.max_columns = None
pd.options.display.max_colwidth = None
# generate the dummy data
df = pd.DataFrame({'DateTimeInUTC': pd.date_range('01.01.2000', periods=length, freq='T', tz='UTC'),
                   'TimeZone': [pytz.all_timezones[random.randint(0, ltz)] for tz in range(length)]})

toc = time.perf_counter()
print(f"Generated the df in {toc - tic:0.4f} seconds\n")

tic = time.perf_counter()

df['Converted'] = df.groupby('TimeZone')['DateTimeInUTC'].apply(lambda x: x.dt.tz_convert(x.name).dt.tz_localize(None))

print(df)

toc = time.perf_counter()
print(f"\nConverted the df in {toc - tic:0.4f} seconds")

輸出:

Generated the df in 6.3333 seconds

                    DateTimeInUTC            TimeZone           Converted
0       2000-01-01 00:00:00+00:00      Asia/Qyzylorda 2000-01-01 05:00:00
1       2000-01-01 00:01:00+00:00     America/Moncton 1999-12-31 20:01:00
2       2000-01-01 00:02:00+00:00     America/Cordoba 1999-12-31 21:02:00
3       2000-01-01 00:03:00+00:00        Africa/Dakar 2000-01-01 00:03:00
4       2000-01-01 00:04:00+00:00      Pacific/Wallis 2000-01-01 12:04:00
...                           ...                 ...                 ...
6999995 2013-04-23 02:35:00+00:00      America/Guyana 2013-04-22 22:35:00
6999996 2013-04-23 02:36:00+00:00  America/St_Vincent 2013-04-22 22:36:00
6999997 2013-04-23 02:37:00+00:00             MST7MDT 2013-04-22 20:37:00
6999998 2013-04-23 02:38:00+00:00  Antarctica/McMurdo 2013-04-23 14:38:00
6999999 2013-04-23 02:39:00+00:00    America/Atikokan 2013-04-22 21:39:00

[7000000 rows x 3 columns]

Converted the df in 4.1579 seconds

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM