繁体   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