无法转换Pandas数据帧时间戳 [英] Failing to convert Pandas dataframe timestamp

查看:886
本文介绍了无法转换Pandas数据帧时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很擅长与Pandas合作,并试图弄清楚为什么这个时间戳不会转换。例如,一个单独的时间戳是字符串'2010-10-06 16:38:02'。代码如下所示:

I'm pretty new to working with Pandas and am trying to figure out why this timestamp won't convert. As an example, one individual timestamp is the string '2010-10-06 16:38:02'. The code looks like this:

newdata = pd.DataFrame.from_records(data, columns = ["col1", "col2", "col3", "timestamp"], index = "timestamp")
newdata.index = newdata.index.tz_localize('UTC').tz_convert('US/Eastern')

并收到此错误:

AttributeError: 'Index' object has no attribute 'tz_localize'

有人评论这里 tz_localize不是Index类型可用的方法,所以我尝试将其转换为列相反,但是给出了错误

Someone commented here that tz_localize is not a method available to Index types, so I tried converting it as a column instead but that gave the error

TypeError: index is not a valid DatetimeIndex or PeriodIndex

然后我发现此网站,其中仅显示tz_localize ac无论如何,在指数上。

And then I found this site, which says tz_localize only acts on the index, anyway.

如果有人能帮助我,我将不胜感激!我正在使用Pandas 0.15.2。我相信这段代码可能适用于早期版本的其他人,但我无法切换。

If anyone could help me out it would be much appreciated! I'm using Pandas 0.15.2. I believe this code may have worked for someone else with an earlier version, but I can't switch.

编辑:

好了一下后我发现这不会引起任何错误,似乎在短期内做了我想要的事情: newdata.index = pd.DatetimeIndex(newdata.index ).tz_localize('UTC')。tz_convert('US / Eastern')

Ok after messing around a little I found that this doesn't throw any errors and seemed to do what I want in the short-term: newdata.index=pd.DatetimeIndex(newdata.index).tz_localize('UTC').tz_convert('US/‌​Eastern')

推荐答案

我被要求添加正式答案而不是仅仅编辑我的问题,所以在这里。请注意,它建立了上面的答案,但那个对我不起作用。

I've been asked to add a formal answer instead of just editing my question, so here it is. Note it builds off the answer above, but that that one didn't quite work for me.

newdata.index = pd.DatetimeIndex(newdata .index).tz_localize('UTC')。tz_convert('US / Eastern')

这篇关于无法转换Pandas数据帧时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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