简体   繁体   English

将 DateTimeIndex 列标题转换为日期

[英]Convert DateTimeIndex column headings to Date

I've got a dataframe which uses DateTimeIndex for column headings.我有一个 dataframe 它使用 DateTimeIndex 作为列标题。 These are currently displaying in the format 2021-05-01 00:00:00.这些当前以 2021-05-01 00:00:00 的格式显示。 How can I get rid of the "00:00:00"?我怎样才能摆脱“00:00:00”?

I've looked up a few similar questions here on SO and the general response is using.dt.date:我在 SO 上查找了一些类似的问题,一般的回答是 using.dt.date:

df.columns = df.columns.dt.date

but it gives the error AttributeError: 'DatetimeIndex' object has no attribute 'dt'.但它给出了错误 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?我有什么问题或者 dos.dt 只是不适用于我所拥有的日期格式吗?

Use DatetimeIndex.date :使用DatetimeIndex.date

df.columns = df.columns.date

because .dt is used for working with columns ( Series ):因为.dt用于处理列( Series ):

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

ser = ser.dt.date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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