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

后端 未结 4 829
花落未央
花落未央 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:44

    The easiest way is to use to_datetime:

    df['col'] = pd.to_datetime(df['col'])
    

    It also offers a dayfirst argument for European times (but beware this isn't strict).

    Here it is in action:

    In [11]: pd.to_datetime(pd.Series(['05/23/2005']))
    Out[11]:
    0   2005-05-23 00:00:00
    dtype: datetime64[ns]
    

    You can pass a specific format:

    In [12]: pd.to_datetime(pd.Series(['05/23/2005']), format="%m/%d/%Y")
    Out[12]:
    0   2005-05-23
    dtype: datetime64[ns]
    
    0 讨论(0)
  • 2020-11-22 10:45

    You can use the following if you want to specify tricky formats:

    df['date_col'] =  pd.to_datetime(df['date_col'], format='%d/%m/%Y')
    

    More details on format here:

    • Python 2 https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
    • Python 3 https://docs.python.org/3.7/library/datetime.html#strftime-strptime-behavior
    0 讨论(0)
  • 2020-11-22 10:50

    If you have a mixture of formats in your date, don't forget to set infer_datetime_format=True to make life easier

    df['date'] = pd.to_datetime(df['date'], infer_datetime_format=True)

    Source: pd.to_datetime

    or if you want a customized approach:

    def autoconvert_datetime(value):
        formats = ['%m/%d/%Y', '%m-%d-%y']  # formats to try
        result_format = '%d-%m-%Y'  # output format
        for dt_format in formats:
            try:
                dt_obj = datetime.strptime(value, dt_format)
                return dt_obj.strftime(result_format)
            except Exception as e:  # throws exception when format doesn't match
                pass
        return value  # let it be if it doesn't match
    
    df['date'] = df['date'].apply(autoconvert_datetime)
    
    0 讨论(0)
  • 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

    <class 'pandas._libs.tslib.Timestamp'> 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.

    0 讨论(0)
提交回复
热议问题