我有一个pandas数据帧:
Date Time Open High Low Close Volume OpenInt
0 2017-11-17 15:35:00 68.5300 68.7200 68.3800 68.6700 79411 0
1 2017-11-17 15:40:00 68.5956 68.6900 68.5600 68.5900 10014 0
2 2017-11-17 15:45:00 68.5700 68.6700 68.5100 68.6200 14182 0
3 2017-11-17 15:50:00 68.5900 68.6200 68.4900 68.5800 15756 0
4 2017-11-17 15:55:00 68.5500 68.6100 68.5100 68.5500 15984 0
我只想将Time
列的时区从以色列更改为UTC。使用该列的所有行执行此操作的最简单,最快捷的方法是什么?我需要多次循环,仅供参考。
谢谢!
答案 0 :(得分:2)
假设Date
和Time
属于object
dtype:
In [54]: df['Date'] = (pd.to_datetime(df['Date'] + ' ' + df.pop('Time'))
.dt.tz_localize('Israel')
.dt.tz_convert('UTC'))
In [55]: df
Out[55]:
Date Open High Low Close Volume OpenInt
0 2017-11-17 13:35:00+00:00 68.5300 68.72 68.38 68.67 79411 0
1 2017-11-17 13:40:00+00:00 68.5956 68.69 68.56 68.59 10014 0
2 2017-11-17 13:45:00+00:00 68.5700 68.67 68.51 68.62 14182 0
3 2017-11-17 13:50:00+00:00 68.5900 68.62 68.49 68.58 15756 0
4 2017-11-17 13:55:00+00:00 68.5500 68.61 68.51 68.55 15984 0
答案 1 :(得分:1)
也许这对你有用
>>> new_dt = pd.to_datetime(df2.Date + " " + df2.Time).dt.tz_localize('Israel').dt.tz_convert('UTC')
>>> df.Date = new_dt.transform(lambda k:k.date())
>>> df.Time = new_dt.transform(lambda k:k.time())
Date Time Open High Low Close Volume OpenInt DateTime
0 2017-11-17 13:35:00 68.5300 68.72 68.38 68.67 79411 0 2017-11-17 15:35:00
1 2017-11-17 13:40:00 68.5956 68.69 68.56 68.59 10014 0 2017-11-17 15:40:00
2 2017-11-17 13:45:00 68.5700 68.67 68.51 68.62 14182 0 2017-11-17 15:45:00
3 2017-11-17 13:50:00 68.5900 68.62 68.49 68.58 15756 0 2017-11-17 15:50:00
4 2017-11-17 13:55:00 68.5500 68.61 68.51 68.55 15984 0 2017-11-17 15:55:00
这里的不同之处在于您可以同时保留Date
和Time
列。