title: I have weekly satellite data, but I want to convert it to monthly data. How would I do that?
body: <p>I have weekly satellite data and I want to convert it to monthly data, where it's months, longitude, and latitude from 1993 (january) - 2019 (december).</p>
  <p>I originally made a for loop to just take the mean of every 4 weeks to get the monthly means:</p>
  <pre><code>sss_md_monthly = []
  
  weeks = sss_md.time.size//4 
  for i in range(weeks):   
      sss_md_monthly.append(np.mean(sss_md[i::4],axis=0))
      
  sss_md_monthly = np.array(sss_md_monthly)
  </code></pre>
  <p>However, I noticed that there are leap years and particular months that have 5 weeks instead of 4 weeks per month, so my for loop to get the monthly means is not correct since I am taking the means of every 4 weeks (one month), but some months may have 5 weeks instead of 4.</p>
  <pre><code>time = np.array(sss_md.time) #making time array
  
  for i in range(int(len(time)/4)):
      print(time[i*4:(i+1)*4]) # printing the time step for every 4 weeks 
  
  ['1993-01-06T12:00:00.000000000' '1993-01-13T12:00:00.000000000'
   '1993-01-20T12:00:00.000000000' '1993-01-27T12:00:00.000000000'] #all of january 1993
  ['1993-02-03T12:00:00.000000000' '1993-02-10T12:00:00.000000000'
   '1993-02-17T12:00:00.000000000' '1993-02-24T12:00:00.000000000'] # all of february 1993
  ['1993-03-03T12:00:00.000000000' '1993-03-10T12:00:00.000000000'
   '1993-03-17T12:00:00.000000000' '1993-03-24T12:00:00.000000000'] # MARCH 1993 has 5 weeks instead of 4
  ['1993-03-31T12:00:00.000000000' '1993-04-07T12:00:00.000000000'
   '1993-04-14T12:00:00.000000000' '1993-04-21T12:00:00.000000000']
  ['1993-04-28T12:00:00.000000000' '1993-05-05T12:00:00.000000000'
   '1993-05-12T12:00:00.000000000' '1993-05-19T12:00:00.000000000']
  ['1993-05-26T12:00:00.000000000' '1993-06-02T12:00:00.000000000'
   '1993-06-09T12:00:00.000000000' '1993-06-16T12:00:00.000000000']
  ['1993-06-23T12:00:00.000000000' '1993-06-30T12:00:00.000000000'
   '1993-07-07T12:00:00.000000000' '1993-07-14T12:00:00.000000000']
  ....
  </code></pre>
  <p>How would I convert weekly data into the proper monthly time series when there are leap years or certain months with more weeks than others?</p>
  <p>Someone kindly suggested this:</p>
  <pre><code>import datetime
  from datetime import datetime as dt
  import numpy as np
  time = [datetime.datetime.strptime(n[:10],&quot;%Y-%m-%d&quot;) for n in time] # time = np.array(sss_md.time)
  
  month, year = time[0].month, time[0].year
  group_month = {}
  for i in time:
      if (i.month, i.year) in group_month:
         group_month[(i.month, i.year)].append(i)
      else:
         group_month[(i.month, i.year)] = i
  print(group_month)
  </code></pre>
  <p>However I get an error:</p>
  <pre><code>---------------------------------------------------------------------------
  IndexError                                Traceback (most recent call last)
  &lt;ipython-input-178-cb22eada7b48&gt; in &lt;module&gt;
        2 from datetime import datetime as dt
        3 import numpy as np
  ----&gt; 4 time = [datetime.datetime.strptime(n[:10],&quot;%Y-%m-%d&quot;) for n in time]
        5 
        6 month, year = time[0].month, time[0].year
  
  &lt;ipython-input-178-cb22eada7b48&gt; in &lt;listcomp&gt;(.0)
        2 from datetime import datetime as dt
        3 import numpy as np
  ----&gt; 4 time = [datetime.datetime.strptime(n[:10],&quot;%Y-%m-%d&quot;) for n in time]
        5 
        6 month, year = time[0].month, time[0].year
  
  IndexError: invalid index to scalar variable.
  </code></pre>
  <p>Is this error from the structure of my time variable?</p>