Search code examples
pythonpandasdatedatetimeindex

Convert DateTimeIndex column headings to Date


I've got a dataframe which uses DateTimeIndex for column headings. These are currently displaying in the format 2021-05-01 00:00:00. How can I get rid of the "00:00:00"?

I've looked up a few similar questions here on SO and the general response is using .dt.date:

df.columns = df.columns.dt.date

but it gives the error AttributeError: 'DatetimeIndex' object has no attribute 'dt'.

Do I have something wrong or does .dt just not work for format of dates that I've got?


Solution

  • Use DatetimeIndex.date:

    df.columns = df.columns.date
    

    because .dt is used for working with columns (Series):

    df['col'] = df['col'].dt.date
    

    ser = ser.dt.date