Python ';指数';对象没有属性';tz#u本地化';

Python ';指数';对象没有属性';tz#u本地化';,python,pandas,Python,Pandas,我正在尝试转换csv文件中时间/日期列('Created_At')中的所有'GMT'时间实例,以便将其全部格式化为'EST'。 请参阅下文: import pandas as pd from pandas.tseries.resample import TimeGrouper from pandas.tseries.offsets import DateOffset from pandas.tseries.index import DatetimeIndex cambridge = pd.re

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

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:“Index”对象没有属性“tz_localize”

我尝试过各种不同的方法,但对于索引对象无法识别tz_属性的原因,我感到困惑。非常感谢你的帮助

更换

cambridge.set_index('Created_At', drop=False, inplace=True)


嗯。就像另一个tz_本地化当前问题一样,这对我来说很好。这对你有用吗?我从您的示例中简化了一些调用:

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


tz_localize
不是可用于
Index
类型的方法,您能否在将其设置为索引之前执行转换表示,它是可用于
DatetimeIndex
的方法,因此这可能是一个错误?与Thank Ed相比,我是python新手——如果这是一个bug,我将如何修复它?在尝试本地化之前,检查它是索引还是DatetimeIndex;向我们展示一个三行示例,其中包含您开始使用的值(最好采用我们可以用作DataFrame参数的格式);看看适合我的简化版是否适合你。嗨,谢谢你的帮助。当我尝试替换该行时,我得到以下错误:文件“C:\Python27\lib\site packages\pandas-0.15.2-py2.7-win-amd64.egg\pandas\tseries\tools.py”,第467行,在parse\u time\u字符串raise DateParseError(e)pandas.tseries.tools.DateParseError:未知字符串中format@cpg25你能在
cambridge['Created_At']中附上一份数据样本吗
?它看起来像这样吗?
Fri Mar 06 09:30:56+0000 2015
?它不是,它看起来像这样:
“创建时间”:{“$date”:“2015-03-03T18:44:46.000-0500”}“创建时间”:{“$date”:“2015-03-03T18:44:48.000-0500”}“创建时间”:{“$date 2015-03-03T18:44:54.000-0500”}
您要传递的数据中是否有“created_at”字符串?还是口述条目?因为熊猫可能不会自动转换它们,你可能需要清理一下。嗨@cphlewis,谢谢你的帮助。我尝试了你提供的代码,它成功了!!你介意把它评论一下吗?这样我就可以了解你所做的事情了?我最困惑的是
(randn(3,3)
部分。您没有发布数据,所以我使用了随机值。评论。
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>