'How to interpolate a multidimensional xarray?
I read a netCDF file using xarray. The dataset contains lat, lon information of the population for the years: 1975, 1990, 2000 and 2015.
The dataset looks like the following and I made it also available here:
import xarray as xr
ds = xr.open_dataset('borneo_pop_t.nc')
ds
For each pixel I would like to have the information of each year between 1975 and 2000 given the trend of the data points I have. In particular I would like to generate more information and different layers of the population for the missing years.
How can I do that?
Solution 1:[1]
You can use xarray's interpolation function.
Using your variable names,
import pandas as pd
# Create time series of 1x a year data
# (you can use any date range; you can generate pretty much
# any sequence you need with pd.date_range())
dates = pd.date_range('1975-01-01','2015-01-01',freq='1Y')
# Linear interpolation
ds_interp = ds.interp(time=dates)
Note a few things:
- This code just generates a simple linear interpolation, though
ds.interp()supports everything thatscipy.interpolate.interp1d()does - e.g., cubic, polynomial, etc. interpolation. Check the docks linked above for examples. - This of course doesn't create new information; you still only "know" the population at the original points. Be wary what consequences interpolating the data will have on your understanding of what the population actually was in a given year.
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 | ks905383 |

