简体   繁体   中英

how to check if date lies within week range?

So I need to return a boolean True or false if a Exit date lies within a week range so I can then go on to sum a booking value for a given Exit date

week_start      week_end

0 2019-05-01    2019-05-08

1 2019-05-08    2019-05-15

2 2019-05-15    2019-05-22

3 2019-05-22    2019-05-29

4 2019-05-29    2019-06-05

week_start    datetime64[ns]

week_end      datetime64[ns]

Does the below exit date lie within the given week ranges above if so sum booking value ^^

Exit_date = [ '2019-05-19','2019-05-27', '2019-05-26', '2019-05-28', '2019-05-27','2019-05-27', '2019-05-22', '2019-05-18', '2019-05-25',  '2019-05-25',  '2019-05-17', ' 2019-05-25']

booking_cost = ['113.3250','68.3250', '62.4900','80.9917', '79.9900', '41.6600', '50.8250','41.6600', '50.8250','68.3200','68.3200','114.9920']

data ={'Exit_date': Exit_date, 'booking_cost': booking_cost}


Exit_df = pd.DataFrame(data, columns=['Exit_date','booking_cost' ]) # exit date date frame
Exit_df['Exit_date'] = pd.to_datetime(Exit_df['Exit_date'])

print(Exit_df.head())
years = int('2019') #yyyy
months = int('05') #mm
dayss = int('01')   #dd
Start_Date = datetime.date(years, months, dayss)#user selects date


n = [0,1,2,3,4]
week_start=[]
week_end=[]
for elements in n:      #loop to create 4 week period from selected date
    week_n_start = Start_Date
    week_n_end = Start_Date + timedelta(days=7)

    week_start.append(week_n_start)
    week_end.append(week_n_end)

    Start_Date=week_n_end


user_dates =  {'week_start' : week_start , 'week_end' :week_end}


daterange_df = pd.DataFrame(user_dates) # creates data frame of 4 week period
daterange_df['week_start'] = pd.to_datetime(daterange_df['week_start'])
daterange_df['week_end'] = pd.to_datetime(daterange_df['week_end'])

print(daterange_df)

So if the Exit Date lies with in the week range I will then want to sum the booking value ... something like this

Total_Sales =0
if daterange_df['week_start'] <= parking_master['Exit_Date'] <= daterange_df['week_end'] :
  Total_Sales=+Exit_df['Booking Cost']
print(Total_Sales)

Thanks

Use Series.between in list comprehension with sum for count True s - matched Exit_Date values:

z = zip(daterange_df['week_start'], daterange_df['week_end'])
daterange_df['count'] = [parking_master['Exit_Date'].between(s, e).sum() for s, e in z]
print (daterange_df)
  week_start   week_end  count
0 2019-05-01 2019-05-08      0
1 2019-05-08 2019-05-15      0
2 2019-05-15 2019-05-22      4
3 2019-05-22 2019-05-29     12
4 2019-05-29 2019-06-05      0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM