environmentaltools.spatial.fillna
- environmentaltools.spatial.fillna(data, var_='z', method='nearest')[source]
Fill missing values in spatial data using interpolation from valid data.
Replaces NaN values in specified variable using spatial interpolation based on non-missing neighboring values. Useful for gap-filling in spatial datasets.
- Parameters:
data (pd.DataFrame) – Spatial data containing columns ‘x’, ‘y’, and the variable to fill. Missing values should be NaN.
var (str, optional) – Name of the variable column to fill. Default is ‘z’.
method (str, optional) – Interpolation method: ‘nearest’, ‘linear’, or ‘cubic’. Default is ‘nearest’. ‘nearest’ is robust and works well for filling gaps. See scipy.interpolate.griddata for details.
- Returns:
Input data with NaN values filled in the ‘z’ column using spatial interpolation from valid data points.
- Return type:
pd.DataFrame
Notes
The function uses scipy.interpolate.griddata to perform the spatial interpolation. Only locations with NaN values are updated; existing valid data is preserved.
Examples
>>> import pandas as pd >>> import numpy as np >>> data = pd.DataFrame({ ... 'x': [0, 1, 2, 0, 1, 2], ... 'y': [0, 0, 0, 1, 1, 1], ... 'z': [10, np.nan, 30, 40, 50, np.nan] ... }) >>> filled = fillna(data, var_='z', method='nearest')