1

I have a bunch of events with their occurrence date in milliseconds (Epoch). I need to put these events in buckets based on the week of the year. I only need a count of the incidents in a given week. For instance:

2021:
week 1 - 5 incidents
week 2 - 10 incidents

Questions I have is:

  1. What is the easiest data structure to store this? Would it be a list of tuples? For instance:

    2021_weeks_list = []
     2021_weeks_list[0] = ('w1',5)
     2021_weeks_list[1] = ('w2',10)
    

Or is there another data structure that would suit this more?

  1. How do I get the week of a date? For instance 1612805820000 is "Monday, February 8, 2021 9:37:00 AM" and that would be week 7 of 2021. I am not sure if there is a Python library that will return a week when given a date.
Bruce Banner
  • 193
  • 1
  • 12
  • Is a `pandas.DataFrame` an option for you? This is a very powerful and flexible data structure, and excellent for analysis. [Docs here](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) – S3DEV Apr 27 '21 at 21:20

1 Answers1

1

datetime has a function to get the week number from a date, see: How to get week number in Python? for details.

If you only need a count of values per-particular week, you could then use this index into a list, and increment integers stored in that list. The maximum length of the list would obviously be 52 elements (maybe 53 if you wanted to handle some error state where the date couldn't be parsed).

anax32
  • 69
  • 2