How to apply tz_convert with different timezones t

2019-05-25 04:57发布

问题:

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

The output of this:

                        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?

回答1:

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

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.

You almost always a single dtyped column of a single timezone.