Home > Blockchain >  Filtering dataframe given a list of dates
Filtering dataframe given a list of dates

Time:12-22

I have the following dataframe:

            Date            Site
0   1999-10-01 12:00:00      65
1   1999-10-01 16:00:00      21
2   1999-10-02 11:00:00      57
3   1999-10-05 12:00:00      53
4   1999-10-10 16:00:00      43
5   1999-10-24 07:00:00      33
6   1999-10-24 08:00:00      21

I have a datetime list that I get from tolist() in another dataframe.

[Timestamp('1999-10-01 00:00:00'),
 Timestamp('1999-10-02 00:00:00'),
 Timestamp('1999-10-24 00:00:00')]

The tolist() purpose is to filter the dataframe based on the dates inside the list. The end result is:

            Date            Site
0   1999-10-01 12:00:00      65
1   1999-10-01 16:00:00      21
2   1999-10-02 11:00:00      57
5   1999-10-24 07:00:00      33
6   1999-10-24 08:00:00      21

Where only 1st, 2nd and 24th Oct rows will appear in the dataframe.

What is the approach to do this? I have looked up and only see solution to filter between dates or a singular date.

Thank you.

CodePudding user response:

If want compare Timestamp without times use Series.dt.normalize:

df1 = df[df['Date'].dt.normalize().isin(L)]

Or Series.dt.floor :

df1 = df[df['Date'].dt.floor('d').isin(L)]

For compare by dates is necessary convert also list to dates:

df1 = df[df['Date'].dt.date.isin([x.date for x in L])]

print (df1)
                 Date  Site
0 1999-10-01 12:00:00    65
1 1999-10-01 16:00:00    21
2 1999-10-02 11:00:00    57
5 1999-10-24 07:00:00    33
6 1999-10-24 08:00:00    21
  • Related