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.

Amartin
  • 337
  • 2
  • 17

2 Answers2

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

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]
SantiStSupery
  • 202
  • 3
  • 14
  • 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 – Amartin Oct 29 '18 at 15:28
  • 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](https://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops) – SantiStSupery Oct 29 '18 at 15:55