Reputation: 4928
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?
Upvotes: 2
Views: 2570
Reputation: 5684
Or using str.contains
df[~df["time"].str.contains('|'.join(date_list))]
Upvotes: 0
Reputation: 150735
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)]
Upvotes: 4