Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. # flatten the groupby
  2. flat_week_groups = week_groups.reset_index()
  3. # create an index of all the weeks from start_date to now_date
  4. # to pick up the missing weeks
  5. dummy_index = []
  6. for dtime in rrule.rrule(rrule.WEEKLY, dtstart=start_date, until=now_date):
  7. dt_week = dtime.isocalendar()[1]
  8. dt_year = dtime.isocalendar()[0]
  9. dummy_index.append(tuple([dt_year, dt_week]))
  10. # add the missing weeks to flat_week_groups
  11. for i in dummy_index:
  12. if i not in week_groups.index:
  13. flat_week_groups = flat_week_groups.append( \\
  14. {'date_year': i[0], \\
  15. 'date_week': i[1], \\
  16. 'value': 0},ignore_index=True)
  17. # sort by year and week
  18. # re-stack the DataFrame with the added values for empty weeks
  19. week_groups = flat_week_groups.sort_values( \\
  20. ['date_year','date_week']).set_index(['date_year','date_week'])
  21. week_groups.plot(kind='bar',figsize=(10,5),legend=None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement