Home > database >  how to modify for loop that will run over next few days instead of just one
how to modify for loop that will run over next few days instead of just one

Time:04-05

I have the following code working, but at the moment is giving me the exact date match only. What I need is to get the match on the exact date from party dataframe plus 3 next days.

sample of columns from two datasets

  1. Party
  2. Event:

enter image description here

Here is the code:

event = []


for date in party['date']:
       event.append(date in travel['date'].unique())

event = pd.Series(event)

Output is True or False if there is match or not.

I need to find the match of travel date with range of dates from party (date 3 days).

How to modify that loop to run through party['date'] 3 next following days.

CodePudding user response:

This will give you the date three days ahead of the date provided.

import datetime

start_date = "2012-01-01"

new_date = datetime.datetime.strptime(start_date, "%Y-%m-%d").date()

end_date = new_date   datetime.timedelta(days=3)

# output: 2012-01-04
print(end_date)

CodePudding user response:

I have found solution to my problem, (I am sorry if my problem description wasn't clear):

event = []

for date in party['date']:
    event.append(date in travel['date'].unique() 
                     or date   timedelta(days=1) in travel['date'].unique()
                     or date   timedelta(days=2) in travel['date'].unique()
                     or date   timedelta(days=3) in travel['date'].unique())


event = pd.Series(event)
  • Related