'Calculating monthly mean from daily netcdf file in python

Hello I have a netcdf file with daily data. Shape of the file is (5844, 89, 89) i.e 16 years data. I tried to get monthly average from daily data. I am looking for simillar to resample function in pandas dataframe. Is there anyways to do that in python. As I know it is very easy to calculate by using cdo and nco but I am looking in python.

Sample code that I used to read netcdf file is:

import netCDF4
from netCDF4 import Dataset
fh = Dataset(ncfile, mode='r')
time = fh.variables['time'][:]
lon = fh.variables['longitude'][:]
lat = fh.variables['latitude'][:]
data = fh.variables['t2m'][:]
data.shape


Solution 1:[1]

@ jhamman Thank you for suggesting xarray.resample. It is simpler than I thought and the answer to my question is:

import xarray as xr
ds = xr.open_dataset(nc_file)
monthly_data = ds.resample(freq = 'm', dim = 'time', how = 'mean')

Solution 2:[2]

by new versions of xarray, the usage is much more simple as follows

monthly_data=ds.resample(time='m').mean()

Solution 3:[3]

If you are working in Linux or macOS this can be easily done using nctoolkit, which uses CDO as a backend. (Installation instructions here).

If you want to get the monthly mean, you just need the following:

import nctoolkit as nc
data = nc.open_data(ncfile)
data.tmean(["year", "month"])

This can be plotted:

data.plot()

If you wanted to convert it to a pandas dataframe:

df = data.to_dataframe()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tsherwen
Solution 2
Solution 3