i4cfutures
i4cfutures

Reputation: 53

Converting datetime formatted index to date only python pandas

I have a dataframe with an index formatted in datetime as such:

2019-01-01 00:00:00+00:00
2019-01-02 00:00:00+00:00
2019-01-03 00:00:00+00:00   

I would like to only keep the year - month - day component without having to change the index back to a column and then back to index

Desired result:

2019-01-01
2019-01-02
2019-01-03

I have been struggling for quite some time. Hope this hasn't been asked before. Thanks for your input! :)

Upvotes: 1

Views: 1538

Answers (2)

BENY
BENY

Reputation: 323226

Try with

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

Also if in the index

df.index = df.index.date

Upvotes: 2

Ynjxsjmh
Ynjxsjmh

Reputation: 30032

Use pandas.DatetimeIndex.date

df.index = df.index.date

If your index is string not datetime type, convert it to datetime with

df.index = pd.to_datetime(df.index)

Upvotes: 3

Related Questions