'Index' object has no attribute 'tz_localize'

后端 未结 2 1373
小蘑菇
小蘑菇 2021-01-19 05:56

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:



        
相关标签:
2条回答
  • 2021-01-19 06:17

    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>
    
    0 讨论(0)
  • Replace

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

    with

    cambridge.set_index(pd.DatetimeIndex(cambridge['Created_At']), drop=False, inplace=True)
    
    0 讨论(0)
提交回复
热议问题