Convert DataFrame column type from string to datetime, dd/mm/yyyy format

后端 未结 4 891
花落未央
花落未央 2020-11-22 10:13

How can I convert a DataFrame column of strings (in dd/mm/yyyy format) to datetimes?

4条回答
  •  半阙折子戏
    2020-11-22 10:59

    If your date column is a string of the format '2017-01-01' you can use pandas astype to convert it to datetime.

    df['date'] = df['date'].astype('datetime64[ns]')

    or use datetime64[D] if you want Day precision and not nanoseconds

    print(type(df_launath['date'].iloc[0]))

    yields

    the same as when you use pandas.to_datetime

    You can try it with other formats then '%Y-%m-%d' but at least this works.

提交回复
热议问题