Python 熊猫,pytz-简单时区转换

Python 熊猫,pytz-简单时区转换,python,pandas,pytz,Python,Pandas,Pytz,我有以下意见: {'actual':{Timestamp('2013-02-20 13:30:00'):0.93000000000000005} 我将其更改为df: df= pd.DataFrame.from_dict(dict, orient='columns', dtype=None) 如果我执行: df.index = df.index.tz_localize('US/Eastern').tz_convert(pytz.utc) df时间戳已正确转换并返回:

我有以下意见:

{'actual':{Timestamp('2013-02-20 13:30:00'):0.93000000000000005}

我将其更改为df:

df= pd.DataFrame.from_dict(dict, orient='columns', dtype=None)
如果我执行:

df.index = df.index.tz_localize('US/Eastern').tz_convert(pytz.utc)
df时间戳已正确转换并返回:

                           actual
2013-02-20 18:30:00+00:00    0.93
                           actual
2013-02-20 13:30:00-05:00    0.93
但是,如果将命令拆分为两行,例如:

df.index= df.index.tz_localize('US/Eastern')
df.index.tz_convert(pytz.utc)
它不会被转换并返回:

                           actual
2013-02-20 18:30:00+00:00    0.93
                           actual
2013-02-20 13:30:00-05:00    0.93

有人知道为什么吗?

您只需将索引分配回第二行中的
df

import pytz
df.index = df.index.tz_localize('US/Eastern')
df.index = df.index.tz_convert(pytz.utc)

df
#                         actual
#2013-02-20 18:30:00+00:00  0.93

@轻量级在轨道上比赛:嗨,是的,将把它添加到tagsHmm中好吧-我对熊猫很陌生,但我想说这不是属性修改的标准行为,是吗?这实际上是一种修改属性的标准方式。因为在pandas中,大多数函数都是函数式的,也就是说,除非明确要求,否则它们不会在适当的位置修改对象。好的,我现在明白了。Thx,非常有用。