title: How do I calculate average monthly temperatures per each year separately using numpy?
body: <p>So, I have a dataset with temperatures from 1952 to 2017. I need to calculate average monthly temperatures per each year separately. </p>
  
  <p>Dataset:
  <a href="https://drive.google.com/file/d/1_RZPLaXoKydjjgm4ghkwtbOGWKC4-Ssc/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1_RZPLaXoKydjjgm4ghkwtbOGWKC4-Ssc/view?usp=sharing</a></p>
  
  <pre><code>import numpy as np
  fp = 'data/1091402.txt'
  data = np.genfromtxt(fp, skip_header=2, usecols=(4, 5, 6, 7, 8))
  data_mask = (data&lt;-9998)
  data[data_mask] = np.nan
  date = data[:, 0]
  precip = data[:, 1]
  tavg = data[:, 2]
  tmax = data[:, 3]
  tmin = data[:, 4]
  </code></pre>
  
  <p>Printing the data's first five rows gives the following: (first is date, than precipitation, tavg (temperature average), tmax, and tmin) </p>
  
  <pre><code>[[1.9520101e+07 3.1000000e-01 3.7000000e+01 3.9000000e+01 3.4000000e+01]
   [1.9520102e+07           nan 3.5000000e+01 3.7000000e+01 3.4000000e+01]
   [1.9520103e+07 1.4000000e-01 3.3000000e+01 3.6000000e+01           nan]
   [1.9520104e+07 5.0000000e-02 2.9000000e+01 3.0000000e+01 2.5000000e+01]
   [1.9520105e+07 6.0000000e-02 2.7000000e+01 3.0000000e+01 2.5000000e+01]]
  </code></pre>
  
  <p>Here I remove nan values and the missing data from tavg:</p>
  
  <pre><code>missing_tmax_mask =  ~np.isfinite(tmax)
  np.count_nonzero(missing_tmax_mask)
  tmax_mask = np.isfinite(tmax)
  tmax_clean = tmax[tmax_mask]
  date_clean = date[tmax_mask]
  print (tmax_clean)
  [39. 37. 36. ... 48. 49. 56.]
  </code></pre>
  
  <p>Converting them to int and string again to remove the 'YYYYMMDD.0' and get 'YYYYMMDD'</p>
  
  <pre><code>date_clean_int = date_clean.astype(int)
  date_clean_str = date_clean_int.astype(str)
  </code></pre>
  
  <p>Printing date_clean_str gives the following:</p>
  
  <pre><code>['19520101' '19520102' '19520103' ... '20171001' '20171002' '20171004']
  </code></pre>
  
  <p>Creating a year, a month and a day array in the format 'YYYY' and 'MM' and 'DD':</p>
  
  <pre><code>year = [datenow[0:4] for datenow in date_clean_str]
  year = np.array(year)
  month = [d[4:6] for d in date_clean_str]
  month = np.array(month)
  day = [datenow[6:8] for datenow in date_clean_str]
  day = np.array(day)
  </code></pre>
  
  <p>Printing year, month, and day gives the following: </p>
  
  <pre><code>['1952' '1952' '1952' ... '2017' '2017' '2017']
  ['01' '01' '01' ... '10' '10' '10']
  ['01' '02' '03' ... '01' '02' '04']
  </code></pre>
  
  <p>Here is calculating mean values per month including all years:</p>
  
  <pre><code>means_months = np.zeros(12)
  index = 0
  for month_now in np.unique(month):
      means_months[index] = tmax_clean[(month == month_now) &amp; (year &lt; '2017')].mean()
      index = index + 1
  </code></pre>
  
  <p>Here is calculating per each year:</p>
  
  <pre><code>means_years = np.zeros(65)
  index = 0
  for year_now in np.unique(year):
      means_years[index] = tmax_clean[(year == year_now) &amp; (year &lt; '2017')].mean()
      index = index+1
  </code></pre>
  
  <p>But I want to know how to calculate per every month and separate according to month and year both using numpy and the above code. The total count of values would be 780 = 65 years x 12 months. I would prefer to have an answer in the form as above if possible. Something as:</p>
  
  <pre><code>means_year_month = np.zeros(780)
  index = 0
  for ….
  </code></pre>
  
  <p>This is where I get lost. Maybe using a dictionary with {YYYY: MM...}??? </p>