1

I have a data frame that looks like this:

                       price
Date
2022-01-01 19:20:00    100   
2022-01-01 19:27:00    100
2022-01-02 19:31:00    102

I want the dataframe to only have unique dates:

                       price
Date
2022-01-01 19:20:00    100   
2022-01-02 19:31:00    102

How can I achieve that?

2 Answers 2

1

You can simply use duplicated:

# pre-requisite
df['Date'] = pd.to_datetime(df['Date'])

df[~df['Date'].dt.date.duplicated()]

Or if working with the index:

df[~df.index.to_series().dt.date.duplicated().values]

Output:

                 Date  price
0 2022-01-01 19:20:00    100
2 2022-01-02 19:31:00    102
Sign up to request clarification or add additional context in comments.

Comments

0

You can extract the date from the datetime column using df.Date.dt.date, put that into a new column using assign, and after that use drop_duplicates based on only that column. Last, you might want to drop the newly create column that has only the date information. In code that reads

df = (
    df.assign(new_date=lambda df:df.Date.dt.date)
   .drop_duplicates(subset=["new_date"])
   .drop(columns=["new_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.