1

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! :)

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

Try with

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

Also if in the index

df.index = df.index.date

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.