如何将不同时区的tz_convert应用于 pandas 数据帧中的不同行 [英] How to apply tz_convert with different timezones to different rows in pandas dataframe
问题描述
我正在尝试根据标准为Pandas数据框中的各行设置不同的时区。作为MWE,这是我尝试过的:
I am trying to set different timezones for various rows in a Pandas dataframe based on a criterion. As a MWE, here is what I have tried:
test = pd.DataFrame( data = pd.to_datetime(['2015-03-30 20:12:32','2015-03-12 00:11:11']) ,columns=['time'] )
test['new_col']=['new','old']
test.time=test.set_index('time').index.tz_localize('UTC')
test.loc[test.new_col=='new','time']=test[test.new_col=='new'].set_index('time').index.tz_convert('US/Pacific')
print test
此输出:
time new_col
0 1427746352000000000 new
1 2015-03-12 00:11:11+00:00 old
如您所见,具有更新时区的行将转换为整数。如何正确进行,以便更新的条目是一个日期时间?
As you can see, the row with the updated timezone is converted to an integer. How can I do this properly so that the updated entry is a datetime?
推荐答案
使用0.17.0rc2(0.17.0是release在10月9日),你可以这样做。
Using 0.17.0rc2 (0.17.0 is release on Oct 9), you can do this.
In [43]: test['new_col2'] = [Timestamp('2015-03-30 20:12:32',tz='US/Eastern'),Timestamp('2015-03-30 20:12:32',tz='US/Pacific')]
In [44]: test
Out[44]:
time new_col new_col2
0 2015-03-30 20:12:32+00:00 new 2015-03-30 20:12:32-04:00
1 2015-03-12 00:11:11+00:00 old 2015-03-30 20:12:32-07:00
In [45]: test.dtypes
Out[45]:
time datetime64[ns, UTC]
new_col object
new_col2 object
dtype: object
请注意,混合 / em>强制对象
dtype的列。所以可以做到,但一般不推荐。您将需要单独更改条目。
Note that mixed timezones within a column with force object
dtype. So it can be done, but is generally not recommended. You would need to change entries individually.
您几乎总是单个时区的单个dtyped列。
You almost always a single dtyped column of a single timezone.
这篇关于如何将不同时区的tz_convert应用于 pandas 数据帧中的不同行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!