更改 pandas 中多个日期时间列的时区信息 [英] Change timezone info for multiple datetime columns in pandas

查看:100
本文介绍了更改 pandas 中多个日期时间列的时区信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以将数据帧中的所有时间戳列转换为本地/任何时区?
不是通过逐行显示吗?

Is there a easy way of converting all timestamp columns in a dataframe to local/any timezone? Not by doing it coloumn by coloumn?

推荐答案

您可以有选择地将转换应用于所有datetime列。首先,使用 select_dtypes ,然后调用 apply 内的 tz_convert

You can selectively apply the conversion to all datetime columns. First, select them with select_dtypes, then call tz_convert inside apply:

df2 = df.select_dtypes('datetimetz')
df[df2.columns] = df2.apply(lambda x: x.dt.tz_convert(None))

datetimetz选择具有时区信息的所有datetime列。

tz_convert(无)替换为 tz_convert(您的时区)以转换为其他时区

"datetimetz" selects all datetime columns with timezone information.
Replace tz_convert(None) with tz_convert(your_timezone) to convert to a different timezone.

对于没有任何时区信息的纯日期时间对象,请调用 tz_localize 首先使用您当前的时区,然后再调用 tz_convert

For plain datetime objects without any timezone info, call tz_localize first with your current timezone before calling tz_convert.

这篇关于更改 pandas 中多个日期时间列的时区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆