1

Hello I have this list:

b = [[2018-12-14, 2019-01-11, 2019-01-25, 2019-02-08, 2019-02-22, 2019-07-26],
    [2018-06-14, 2018-07-11, 2018-07-25, 2018-08-08, 2018-08-22, 2019-01-26],
    [2017-12-14, 2018-01-11, 2018-01-25, 2018-02-08, 2018-02-22, 2018-07-26]]

dtype: datetime64[ns]]

and I want to know if it's possible to compare this list of dates with another date. I am doing it like this:

r = df.loc[(b[1] > vdate)]

with:

vdate = dt.date(2018, 9, 19) 

the output is correct because it select the values that satisfy the condition. But the problem is that I want to do that for all the list values. Something like:

r = df.loc[(b > vdate)] # Without [1]

but this get as an output an error as I expected. I try some for loop and it seems like it works but I am not sure:

g = []
for i in range(len(b)):
    r = df.loc[(b[i] > vdate)]
    g.append(r)

Thank you so much for your time and any help would be perfect.

6
  • 1
    You want to have boolean value if your date is < than every dates in your list? Commented Oct 29, 2018 at 14:52
  • 1
    Right now b is a list of lists. You may want to use a for loop to go through individual lists Commented Oct 29, 2018 at 14:53
  • 1
    Question not clear Commented Oct 29, 2018 at 14:54
  • @DawidFieluba no, I want to have the values of the df when my date is < than every date in the list. Thank you for taking your time to read it and help Commented Oct 29, 2018 at 14:58
  • 1
    Then find the minimum date in the list and check if the DataFrame values are less than that single date. Commented Oct 29, 2018 at 15:09

2 Answers 2

1

One may use the apply function as stated by @Joseph Developer, but a simple list comprehension would not require you to write the function. The following will give you a list of boolean telling you whether or not each date is greater than vdate :

is_after_b = [x > vdate for x in b]

And if you want to include this directly in your DataFrame you may write :

df['is_after_b'] = [ x > vdate for x in df.b]

Assuming that b is a column of df, which btw would make sure that the length of b and your DataFrame's columns match.

EDIT

I did not consider that b was a list of list, you would need to flatten b by using :

flat_b = [item for sublist in b for item in sublist]

And you can now use :

is_after_b = [x > vdate for x in flat_b]
Sign up to request clarification or add additional context in comments.

2 Comments

And it is a way to do it if b is not a column of the dataframe? I have a way using iloc[] to get the boolean output as a column but I don't know how to transfor it to the values
I edited my answer, now it should work flawlessly. The list comprehension is faster than looping and using .loc, for more on this aspect see this question
1

if you want to go through the entire list just use the following method:

ds['new_list'] = ds['list_dates'].apply(function)

use the .apply () method to process your list through a function

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.