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

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

    I adopted the dateutil technique earlier today but have since switched to a faster alternative:

    date_parser = lambda ts: pd.to_datetime([s[:-5] for s in ts]))
    

    Edit: s[:-5] is correct (screenshot has error)

    In the screenshot below, I import ~55MB of tab-separated files. The dateutil method works, but takes orders of magnitude longer.

    This was using pandas 0.18.1 and dateutil 2.5.3.


    EDIT This lambda function will work even if Z-0000 suffix is missing...

    date_parser = lambda ts: pd.to_datetime([s[:-5] if 'Z' in s else s for s in ts])
    

提交回复
热议问题