在 pandas 中剥离时区信息 [英] Strip timezone info in pandas

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

问题描述

我一直在努力从熊猫数据框中的列中删除时区信息.我已经检查了以下问题,但是对我来说不起作用:

I have been struggling with removing the time zone info from a column in a pandas dataframe. I have checked the following question, but it does not work for me:

我可以将pandas DataFrame导出到Excel中以剥离tzinfo吗? /a>

Can I export pandas DataFrame to Excel stripping tzinfo?

我使用tz_localize将时区分配给datetime对象,因为我需要使用tz_convert转换为另一个时区.这将以"-06:00"的方式添加UTC偏移量.我需要摆脱此偏移量,因为当我尝试将数据框导出到Excel时会导致错误.

I used tz_localize to assign a timezone to a datetime object, because I need to convert to another timezone using tz_convert. This adds an UTC offset, in the way "-06:00". I need to get rid of this offset, because it results in an error when I try to export the dataframe to Excel.

实际输出

2015-12-01 00:00:00-06:00

所需的输出

2015-12-01 00:00:00

我尝试使用str()方法获取想要的字符,但是tz_localize的结果似乎不是字符串.到目前为止,我的解决方案是将数据帧导出到csv,读取文件,并使用str()方法获取所需的字符.

I have tried to get the characters I want using the str() method, but it seems the result of tz_localize is not a string. My solution so far is to export the dataframe to csv, read the file, and to use the str() method to get the characters I want.

有更简单的解决方案吗?

Is there an easier solution?

推荐答案

也许可以帮助删除最后6个字符:

Maybe help strip last 6 chars:

print df
                    datetime
0  2015-12-01 00:00:00-06:00
1  2015-12-01 00:00:00-06:00
2  2015-12-01 00:00:00-06:00

df['datetime'] = df['datetime'].astype(str).str[:-6]
print df
              datetime
0  2015-12-01 00:00:00
1  2015-12-01 00:00:00
2  2015-12-01 00:00:00

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

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