Having a NetCDF with a 'time', 'lat' and 'lon' dimension, where time is hourly during a year and the variable to extract is temperature, how do I calculate mean temperature throughout the year using Python? (using Python and netcdf4 or xarray or numpy)

  • I would use xarray. Add an additional coordinate/dimension that is "year", and then group by year. Please provide some code/example if you want more details. Cheers! Commented Nov 21, 2019 at 20:43

1 Answer 1


I would use xarray.Dataset.resample. xarray is very handy for time series and comes with several temporal dimension handling methods, some of which taken from the comprehensive pandas. Check the docs here. If this does not solve the issue, please add some more context as Rowan asked in the comment.

import numpy as np
import pandas as pd
import xarray as xr
da = xr.DataArray(np.linspace(0, 11, num=12), coords=[pd.date_range('15/12/1999',
    periods=12, freq=pd.DateOffset(months=1))], dims='time')

Have a look at different frequency offsets you can input as parameters in the .resample method, in the pandas documentation here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.