我很擅长与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数据帧时间戳
发布评论