How to read timezone aware datetimes as a timezone naive local DatetimeIndex with read_csv in pandas?

前端 未结 4 1878
小蘑菇
小蘑菇 2021-01-03 01:21

When I use pandas read_csv to read a column with a timezone aware datetime (and specify this column to be the index), pandas converts it to a timezone naive utc

4条回答
  •  不知归路
    2021-01-03 01:46

    The answer of Alex leads to a timezone aware DatetimeIndex. To get a timezone naive local DatetimeIndex, as asked by the OP, inform dateutil.parser.parser to ignore the timezone information by setting ignoretz=True:

    import dateutil
    
    date_parser = lambda x: dateutil.parser.parse(x, ignoretz=True)
    df = pd.read_csv('Test.csv', index_col=0, parse_dates=True, date_parser=date_parser)
    
    print(df)
    

    outputs

                         Temperature
    DateTime                        
    2016-07-01 11:05:07       21.125
    2016-07-01 11:05:09       21.138
    2016-07-01 11:05:10       21.156
    2016-07-01 11:05:11       21.179
    2016-07-01 11:05:12       21.198
    2016-07-01 11:05:13       21.206
    2016-07-01 11:05:14       21.225
    2016-07-01 11:05:15       21.233
    

提交回复
热议问题