为什么这个“pythonic”代码不适用于tz_localize()
行?
import pandas as pd
df = pd.DataFrame([{ "Localtime":"2016-01-01 12:00:00", "Timezone":"Europe/London" },
{ "Localtime":"2016-01-01 12:00:00", "Timezone":"Europe/Paris" }])
df['UtcDateTime'] = pd.to_datetime(df['Localtime']).tz_localize(df['Timezone'])
df
我正在寻找一些比以下代码更漂亮的代码(工作正常btw):
import pandas as pd
df = pd.DataFrame([{ "Localtime":"2016-01-01 12:00:00", "Timezone":"Europe/London" },
{ "Localtime":"2016-01-01 12:00:00", "Timezone":"Europe/Paris" }])
df['UtcDateTime'] = 'NA'
i=0
for ts in df.Localtime:
ts1 = pd.to_datetime(ts).tz_localize(df.Timezone[i])
df['UtcDateTime'][i] = ts1
i=i+1
df
预期结果:
Localtime Timezone UtcDateTime
0 2016-01-01 12:00:00 Europe/London 2016-01-01 12:00:00+00:00
1 2016-01-01 12:00:00 Europe/Paris 2016-01-01 12:00:00+01:00
答案 0 :(得分:2)
您可能只想选择apply
df.apply(lambda x: pd.to_datetime(x.Localtime).tz_localize(x.Timezone), axis=1)
<强>演示强>
>>> df['UtcDateTime'] = df.apply(lambda x: pd.to_datetime(x.Localtime).tz_localize(x.Timezone), axis=1)
>>> df
Localtime Timezone UtcDateTime
0 2016-01-01 12:00:00 Europe/London 2016-01-01 12:00:00+00:00
1 2016-01-01 12:00:00 Europe/Paris 2016-01-01 12:00:00+01:00
您的简短方法不起作用的原因是tz_localize
确实需要索引来处理,而不是值。我建议你将它转换为类似pd.DatetimeIndex
的DatetimeIndex,
然而,tz_localize
只接受一个时区,因此无论如何你最终会申请过行。
答案 1 :(得分:2)
您的代码无效,因为tz_localize()
期望将单个时区应用于系列中的多个值。为了使你的循环更清洁,你可以使用apply()
:
<强>代码:强>
def localize_ts(row):
return pd.to_datetime(row['Localtime']).tz_localize(row['Timezone'])
df['UtcDateTime'] = df.apply(localize_ts, axis=1)
<强>产地:强>
Localtime Timezone UtcDateTime
0 2016-01-01 12:00:00 Europe/London 2016-01-01 12:00:00+00:00
1 2016-01-01 12:00:00 Europe/Paris 2016-01-01 12:00:00+01:00