Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter dataframe rows using a list of datetime.date objects

Tags:

python

pandas

Issue:

I am trying to filter my dataframe rows using a list of datetime.date objects. I get ValueError: Arrays were different lengths: 175033 vs 33

Data:

type(third_friday_lst)
list

type(third_friday_lst[0])
datetime.date

third_friday_lst

[datetime.date(2013, 1, 18),
 datetime.date(2013, 2, 15),
 datetime.date(2013, 3, 15),
 datetime.date(2013, 4, 19),
 datetime.date(2013, 5, 17),
 datetime.date(2013, 6, 21),
 datetime.date(2013, 7, 19),
 datetime.date(2013, 8, 16),
 datetime.date(2013, 9, 20),
 datetime.date(2013, 10, 18),
 datetime.date(2013, 11, 15)]

What I have tried:

If I use the following code I get the desired output but only when I specify the first item in the list with third_friday_lst[0]:

data[data['dt-date'] == third_friday_lst[0]]

                       Date         Time    Open    High    Low   Last Volume   dt-date
Timestamp                               
2013-01-18 08:00:00 2013/1/18   08:00:00    7875.5  7876.0  7867.5  7870.5 1059 2013-01-18
2013-01-18 08:05:00 2013/1/18   08:05:00    7871.0  7878.5  7870.5  7877.5  511 2013-01-18
2013-01-18 08:10:00 2013/1/18   08:10:00    7877.0  7879.0  7875.5  7875.5  226 2013-01-18
2013-01-18 08:15:00 2013/1/18   08:15:00    7875.5  7878.0  7874.5  7876.0  162 2013-01-18

Desired output:

I would like to filter rows in data using third_friday_lst without using the list index[0] as I want all the dates contained in the list. I would prefer not to use the ['dt-date'] column but if this is required it is fine.

thanks

like image 286
nipy Avatar asked Dec 19 '25 13:12

nipy


1 Answers

You can try:

data[data['dt-date'].isin(third_friday_lst)]

Thanks!

like image 98
Abdou Avatar answered Dec 21 '25 03:12

Abdou