pandas 在熊猫中剥离时区信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34506112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strip timezone info in pandas
提问by Ernesto561
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 数据框中的列中删除时区信息。我已经检查了以下问题,但它对我不起作用:
Can I export pandas DataFrame to Excel stripping tzinfo?
我可以将 Pandas DataFrame 导出到 Excel 剥离 tzinfo 吗?
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.
我使用 tz_localize 将时区分配给 datetime 对象,因为我需要使用 tz_convert 转换为另一个时区。这会以“-06:00”的方式添加 UTC 偏移量。我需要去掉这个偏移量,因为当我尝试将数据框导出到 Excel 时它会导致错误。
Actual output
实际产量
2015-12-01 00:00:00-06:00
Desired output
期望输出
2015-12-01 00:00:00
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.
我试图使用 str() 方法获取我想要的字符,但似乎 tz_localize 的结果不是字符串。到目前为止,我的解决方案是将数据帧导出到 csv,读取文件,然后使用 str() 方法获取我想要的字符。
Is there an easier solution?
有更简单的解决方案吗?
采纳答案by jezrael
Maybe help strip last 6 chars:
也许有助于去除最后 6 个字符:
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
回答by Diego Mora Cespedes
If your series contains only datetimes, then you can do:
如果您的系列仅包含日期时间,那么您可以执行以下操作:
my_series.dt.tz_localize(None)
my_series.dt.tz_localize(None)
This will remove the timezone information ( it will not change the time) and return a series of naive local times, which can be exported to excel using to_excel() for example.
这将删除时区信息(它不会改变时间)并返回一系列朴素的本地时间,例如可以使用 to_excel() 将其导出到 excel。
回答by ryanjdillon
Following Beatriz Fonseca's suggestion, I ended up doing the following:
按照 Beatriz Fonseca 的建议,我最终做了以下事情:
from datetime import datetime
df['dates'].apply(lambda x:datetime.replace(x,tzinfo=None))
回答by Mikhail
To remove timezone from all columns just use:
要从所有列中删除时区,只需使用:
for col in df.select_dtypes(['datetimetz']).columns:
df[col] = df[col].dt.tz_convert(None)
回答by Caridorc
If it is always the last 6 characters that you want to ignore, you may simply slice your current string:
如果始终是您要忽略的最后 6 个字符,则可以简单地对当前字符串进行切片:
>>> '2015-12-01 00:00:00-06:00'[0:-6]
'2015-12-01 00:00:00'