title: xarray computing monthly mean from multiple year netcdf
body: <p>I have a 2m temperature netcdf file from ERA5 that goes from 2000 to 2019 for the months 04 to 10, giving a total of 13680 timesteps and a 61x161 lat-lon dimension. I want to do a monthly mean of all the daily timesteps for each year separately. For example, we would have monthly mean of the data in April of 2000, in May of 2000 and so forth. I've tried the following code with xarray resample, but two problems occur.</p>
  <ol>
  <li>For some reason the mean seems to do the mean for all the years.</li>
  <li>The resample function creates months 01, 02, 03, 11 and 12, despite there being no data for it!</li>
  </ol>
  <p>Here's what I’m talking about:</p>
  <pre><code>import xarray as xr
  ds = xr.open_dataset(netcdf)
  monthly_data=ds.resample(time='1M').mean()
  </code></pre>
  <p>We can look at the timestamp which shows monthly timestep, including non-related months.</p>
  <pre><code>print(np.array(monthly_data.time))
  array(['2000-04-30T00:00:00.000000000', '2000-05-31T00:00:00.000000000',
         '2000-06-30T00:00:00.000000000', '2000-07-31T00:00:00.000000000',
         '2000-08-31T00:00:00.000000000', '2000-09-30T00:00:00.000000000',
         '2000-10-31T00:00:00.000000000', '2000-11-30T00:00:00.000000000',
         '2000-12-31T00:00:00.000000000', '2001-01-31T00:00:00.000000000',
  </code></pre>
  <p>To verify the content of the temperature, I turned the data into a dataframe.</p>
  <pre><code>temp_ar = np.array(monthly_data.t2m)    
  print(pd.DataFrame(temp_ar[0,:,:]).head())
            0           1           2    ...         158         159         160
  0  270.940613  270.911652  270.926727  ...         NaN         NaN         NaN
  1  271.294952  271.256744  271.250946  ...  272.948608  272.974731  272.998535
  2  271.416779  271.457214  271.483459  ...  273.123169  273.079285  273.058563
  3  271.848755  271.791382  271.784058  ...         NaN  273.264038         NaN
  4  272.226837  272.144928  272.123016  ...         NaN         NaN         NaN
  
  print(pd.DataFrame(temp_ar[1,:,:]).head())
     0    1    2    3    4    5    6    ...  154  155  156  157  158  159  160
  0  NaN  NaN  NaN  NaN  NaN  NaN  NaN  ...  NaN  NaN  NaN  NaN  NaN  NaN  NaN
  1  NaN  NaN  NaN  NaN  NaN  NaN  NaN  ...  NaN  NaN  NaN  NaN  NaN  NaN  NaN
  2  NaN  NaN  NaN  NaN  NaN  NaN  NaN  ...  NaN  NaN  NaN  NaN  NaN  NaN  NaN
  3  NaN  NaN  NaN  NaN  NaN  NaN  NaN  ...  NaN  NaN  NaN  NaN  NaN  NaN  NaN
  4  NaN  NaN  NaN  NaN  NaN  NaN  NaN  ...  NaN  NaN  NaN  NaN  NaN  NaN  NaN
  </code></pre>
  <p>The 2nd array (which corresponds to the month 05 of 2000) shouldn't have nans, but it does and its like this for all the other timesteps (except for the last one for some reason). Would anybody know why this is happening?</p>
  <p>Here is the original dataset</p>
  <pre><code>print(ds)
  &lt;xarray.Dataset&gt;
  Dimensions:    (latitude: 61, longitude: 161, time: 13680)
  Coordinates:
    * longitude  (longitude) float32 -80.0 -79.9 -79.8 -79.7 ... -64.2 -64.1 -64.0
    * latitude   (latitude) float32 50.0 49.9 49.8 49.7 ... 44.3 44.2 44.1 44.0
    * time       (time) datetime64[ns] 2000-04-01 ... 2018-10-30T23:00:00
  Data variables:
      t2m        (time, latitude, longitude) float32 ...
  Attributes:
      Conventions:  CF-1.6
      history:      2020-12-07 03:50:31 GMT by grib_to_netcdf-2.16.0: /opt/ecmw...
  </code></pre>
  <p>Any help would be. Maybe I should try some other method?
  Cheers!</p>