“索引"对象没有属性"tz_localize" [英] 'Index' object has no attribute 'tz_localize'

查看:254
本文介绍了“索引"对象没有属性"tz_localize"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在csv文件的时间/日期列("Created_At")中转换"GMT"时间的所有实例,以便将其全部格式化为"EST". 请参见以下内容:

I'm trying to convert all instances of 'GMT' time in a time/date column ('Created_At') in a csv file so that it is all formatted in 'EST'. Please see below:

import pandas as pd
from pandas.tseries.resample import TimeGrouper
from pandas.tseries.offsets import DateOffset
from pandas.tseries.index import DatetimeIndex

cambridge = pd.read_csv('\Users\cgp\Desktop\Tweets.csv')
cambridge['Created_At'] = pd.to_datetime(pd.Series(cambridge['Created_At']))
cambridge.set_index('Created_At', drop=False, inplace=True)
cambridge.index = cambridge.index.tz_localize('GMT').tz_convert('EST')
cambridge.index = cambridge.index - DateOffset(hours = 12)

我得到的错误是:

cambridge.index = cambridge.index.tz_localize('GMT').tz_convert('EST')

AttributeError:索引"对象没有属性"tz_localize"

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

我尝试了各种不同的方法,但是对于为什么Index对象无法识别tz_attribute感到困惑.非常感谢您的帮助!

I've tried various different things but am stumped as to why the Index object won't recognized the tz_attribute. Thank you so much for your help!

推荐答案

嗯.像其他tz_localize当前问题一样,这对我来说很好.这对您有用吗?我已经从您的示例中简化了一些调用:

Hmm. Like the other tz_localize current problem, this works fine for me. Does this work for you? I have simplified some of the calls a bit from your example:

df2 =  pd.DataFrame(randn(3, 3), columns=['A', 'B', 'C'])
# randn(3,3) returns nine random numbers in a 3x3 array.
# the columns argument to DataFrame names the 3 columns. 
# no datetimes here! (look at df2 to check)

df2['A'] = pd.to_datetime(df2['A'])
# convert the random numbers to datetimes -- look at df2 again
# if A had values to_datetime couldn't handle, we'd clean up A first

df2.set_index('A',drop=False, inplace=True)
# and use that column as an index for the whole df2;

df2.index  = df2.index.tz_localize('GMT').tz_convert('US/Eastern')
# make it timezone-conscious in GMT and convert that to Eastern

df2.index.tzinfo

<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>

这篇关于“索引"对象没有属性"tz_localize"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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