2

Using python 3.7, pandas 1.1.1

I have dataframe with datetimeindex and list of date_list =["2020-07-19", "2020-07-24", ... etc]

I want to remove all rows in my dataframe that contain dates in date_list.

My datetimeindex contains variety of hh:mm:ss as well and no matter its time I want to remove if its dates are in date_list

dataframe looks like this:

      time              data
2020-07-19 23:52:02       1
2020-07-20 13:44:02       1
2020-07-22 23:52:02       1
2020-07-24 08:52:02       1
2020-07-24 21:52:02       1

desired output would be:

      time              data
2020-07-20 13:44:02       1
2020-07-22 23:52:02       1

deleting dates in date_list

following How can I delete rows for a particular Date in a Pandas dataframe? I've tried df.drop(pd.to_datetime("2020-07-19")) which give KeyError: "[Timestamp('2020-07-19 00:00:00')] not found in axis"

How can I remove dates without considering its time?

3 Answers 3

4

Since your time are not exact 00:00:00 on the day, you can use normalize() to extract the date. Then you can use isin:

date_list = pd.to_datetime(date_list)

df[~df['time'].dt.normalize().isin(date_list)]

If time is the index:

df[~df.index.normalize().isin(date_list)]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this works but I need to df.reset_index() and then use it, can I do it with index?
Is time column the index of your df?
Amazing! could you explain why we need extra .dt when time is set as column and without .dt when time is set as index?
@Ambleu not sure why. Probably a good question to Pandas' dev. I personally find it confusing sometimes.
2

Check

df[~df.index.to_series().dt.date.isin(date_list)]

3 Comments

Thanks! Though this outputs TypeError: 'Series' object is not callable
@QuangHoang ah that is right ~ I should test before posted, thank you man ~
This outputs AttributeError: 'numpy.ndarray' object has no attribute 'isin'
0

Or using str.contains

df[~df["time"].str.contains('|'.join(date_list))]

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.