fhebe12
fhebe12

Reputation: 69

Python - Iterate back in time and look if DataFrame contains a specific Date

i want to iterate back in time and check if a df contains some dates.

Here's a practical example:

start_date = datetime(2000,1,3)
end_date =datetime(2020,12,31)
index = pd.date_range(start_date, end_date, freq='B')
df = pd.DataFrame({'Date':index})
df

    Date
0   2000-01-03
1   2000-01-04
2   2000-01-05
3   2000-01-06
4   2000-01-07
...     ...
5474    2020-12-25
5475    2020-12-28
5476    2020-12-29
5477    2020-12-30
5478    2020-12-31

5479 rows × 1 columns

As an exercise i've tried to run this loop:

for date in reversed(pd.date_range(start_date,end_date,freq = 'B')):
    if df.Date == date:
        print(date)
    else:
        print("No")

I always get the error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What am i missing here?

Upvotes: 0

Views: 239

Answers (1)

Danail Petrov
Danail Petrov

Reputation: 1875

The problem is that this statement will return Series and not a boolean (True/False), which is why the output is ambiguous

if df.Date == date

Therefore the if condition is unable to determine if this is True or False.

You can do this instead

if any(df.Date == date)

which should give you the expected result.

This is yet not an optimal way of doing it. The right way of getting all dates in one go is using some vectorised / broadcast method such as isin

>>> dates = reversed(pd.date_range(start_date,end_date,freq = 'B'))
>>> df.Date.isin(dates)

This will give you all the dates contained within the dates object. Much more efficiently than looping through every single one of them.

Upvotes: 1

Related Questions