![](/img/trans.png)
[英]tz_localize: KeyError: ('Asia/Singapore', u'occurred at index 0')
[英]'Index' object has no attribute 'tz_localize'
我正在嘗試在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'
我嘗試了各種不同的東西,但我很難過為什么Index對象無法識別tz_attribute。 非常感謝你的幫助!
嗯。 像其他tz_localize當前問題一樣,這對我來說很好。 這對你有用嗎? 我從你的例子中簡化了一些調用:
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>
更換
cambridge.set_index('Created_At', drop=False, inplace=True)
同
cambridge.set_index(pd.DatetimeIndex(cambridge['Created_At']), drop=False, inplace=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.