无法转换Pandas数据帧时间戳

编程入门 行业动态 更新时间:2024-10-26 18:19:35
本文介绍了无法转换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数据帧时间戳

本文发布于:2023-11-23 14:51:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   数据   Pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!