Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. '''
  2. Aggregation of bookings, in a given time interval, aggregated per
  3. #1 -> weekday
  4. #2 -> pre holiday
  5. #3 -> holiday
  6. #4 -> weekend
  7. '''
  8. def query_time_filter_day_type(self, provider, city, start, end, day_type):
  9. books_df = self.get_books("car2go", "torino", start, end)
  10. books_df['b_day'] = books_df['start'].apply(isbday)
  11.  
  12. cal = Italy()
  13. holidays=[]
  14.  
  15. #holidays collection creation
  16. if start.year == end.year :
  17. for h in cal.holidays(start.year):
  18. holidays.append(h[0])
  19. else :
  20. for year in range (start.year, end.year+1):
  21. for h in cal.holidays(year):
  22. holidays.append(h[0])
  23.  
  24. if day_type == 1: #return business days
  25. business_books_df = books_df[books_df['b_day'] == True]
  26. return business_books_df
  27.  
  28. elif day_type == 2 : #only the day BEFORE the holiday. WEEKends are not considered
  29. pre_holidays = []
  30. for d in holidays:
  31. pre_holidays.append(d - datetime.timedelta(days = 1))
  32. pre_holidays.pop()
  33. #ph_day = pre hoiday day
  34. books_df['ph_day'] = books_df['start'].apply(lambda x: x.date())
  35. books_df['ph_day'] = books_df[books_df['ph_day'].isin(pre_holidays)]
  36. pre_holidays_books_df = books_df.dropna(subset=['ph_day'],how='all')
  37. del pre_holidays_books_df['b_day']
  38. del pre_holidays_books_df['ph_day']
  39. return pre_holidays_books_df
  40.  
  41. elif day_type == 3 : # holidays WEEKEND EXCLUDED
  42. books_df['h_day'] = books_df['start'].apply(lambda x: x.date())
  43. books_df['h_day'] = books_df[books_df['h_day'].isin(holidays)]
  44. holidays_books_df = books_df.dropna(subset=['h_day'],how='all')
  45. del holidays_books_df['b_day']
  46. del holidays_books_df['h_day']
  47. return holidays_books_df
  48.  
  49. elif day_type == 4:#returns WEEKENDS
  50. weekend_books_df = books_df[books_df['b_day'] == False]
  51. return weekend_books_df
  52. else:
  53. print "day_type error"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement