简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM