Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Index' object has no attribute 'tz_localize'

Tags:

python

pandas

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)

The error I'm getting is:

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

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

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!

like image 261
cgp25 Avatar asked Mar 06 '15 16:03

cgp25


1 Answers

Replace

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

with

cambridge.set_index(pd.DatetimeIndex(cambridge['Created_At']), drop=False, inplace=True)
like image 67
Mostafa Mahmoud Avatar answered Oct 09 '22 02:10

Mostafa Mahmoud