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

前端 未结 4 1841
小蘑菇
小蘑菇 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:51

    According to the docs the default date_parser uses dateutil.parser.parser. According to the docs for that function, the default is to ignore timezones. So if you supply dateutil.parser.parser as the date_parser kwarg, timezones are not converted.

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

    outputs

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

提交回复
热议问题