'how to modify for loop that will run over next few days instead of just one
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
- Party
- Event:
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.
Solution 1:[1]
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)
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Captain Caveman |
| Solution 2 | Sylwia Kmiec |

