3

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

3
  • 2
    data[data['dt-date'].isin(third_friday_lst)]? Commented Dec 31, 2016 at 0:19
  • 1
    That's what I would say. ^^^ Commented Dec 31, 2016 at 0:22
  • Thanks that did it. If you add the answer I will accept. Commented Dec 31, 2016 at 0:22

2 Answers 2

5

You can try:

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

Thanks!

Sign up to request clarification or add additional context in comments.

Comments

2

Just for completeness, I mentioned in the post that I didn't want to use the ['dt-date'] column:

data[data.index.to_series().dt.date.isin(third_friday_lst)]

2 Comments

Looks like you can also do it in a slightly simpler fashion too: data[data.index.isin(third_friday_lst)]
@EHB actually this is not working as it'll only filter exact match between the index datetimes and the list of dates hence only get rows at midnight

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.