Skip to Content

GRIDDATA

Overview

The GRIDDATA function performs interpolation on unstructured N-dimensional data. It is useful for estimating values at new points based on scattered data, such as in geospatial analysis or scientific computing. The function supports three interpolation methods: linear, nearest, and cubic. The underlying algorithm is provided by SciPy’s scipy.interpolate.griddata function; see the official documentation  for full details. This wrapper exposes the most frequently used arguments and always converts SciPy’s NaN outputs to None so Excel can consume them, while omitting the rescale option available in SciPy.

This example function is provided as-is without any representation of accuracy.

Usage

To use the function in Excel:

=GRIDDATA(points, values, xi, [griddata_method], [fill_value])
  • points (2D list, required): Table of coordinates for the data points. Each row is a point in N dimensions.
  • values (2D column vector, required): Table of values at each data point. Must have the same number of rows as points.
  • xi (2D list, required): Table of coordinates where interpolation is desired. Each row is a point in N dimensions.
  • griddata_method (string (enum), optional, default=“linear”): Interpolation method. Valid options: Linear, Nearest, Cubic.
  • fill_value (float, optional, default=None): Value to use for points outside the convex hull of the input points. When omitted, SciPy’s default is used and the wrapper returns None for out-of-hull evaluations.

The function returns a 2D column vector (list of lists) of interpolated values at the locations specified by xi. Results outside the convex hull are reported as None when no fill_value is supplied. Errors are returned as a 2D list of strings describing the issue.

Examples

Example 1: Linear Interpolation in 2D

This example interpolates values at two new points using three known data points in 2D.

Inputs:

pointsvaluesximethodfill_value
0.00.01.00.20.2linear
1.00.02.00.40.4
0.01.03.0

Excel formula:

=GRIDDATA({0,0;1,0;0,1}, {1;2;3}, {0.2,0.2;0.4,0.4}, "linear")

Expected output:

Result
1.600
2.200

Example 2: Nearest Neighbor Interpolation

This example uses the ‘nearest’ method to interpolate at two points.

Inputs:

pointsvaluesximethodfill_value
0.00.01.00.90.1nearest
1.00.02.00.10.9
0.01.03.0

Excel formula:

=GRIDDATA({0,0;1,0;0,1}, {1;2;3}, {0.9,0.1;0.1,0.9}, "nearest")

Expected output:

Result
2.000
3.000

Example 3: Cubic Interpolation in 2D

This example uses the ‘cubic’ method for interpolation with four non-collinear points.

Inputs:

pointsvaluesximethodfill_value
0.00.01.00.30.3cubic
1.00.02.00.70.7
0.01.03.00.50.5
1.01.04.0

Excel formula:

=GRIDDATA({0,0;1,0;0,1;1,1}, {1;2;3;4}, {0.3,0.3;0.7,0.7;0.5,0.5}, "cubic")

Expected output:

Result
1.900
3.100
2.500

Example 4: Fill Value for Out-of-Bounds

This example shows how to use fill_value for a point outside the convex hull, with four points.

Inputs:

pointsvaluesximethodfill_value
0.00.01.02.02.0linear99
1.00.02.0
0.01.03.0
1.01.04.0

Excel formula:

=GRIDDATA({0,0;1,0;0,1;1,1}, {1;2;3;4}, {2,2}, "linear", 99)

Expected output:

Result
99.000

Python Code

from typing import Any, List, Optional, Union import numpy as np from scipy.interpolate import griddata as scipy_griddata ScalarInput = Union[int, float, bool, str, None] MatrixInput = Union[ScalarInput, List[Any]] GriddataResult = List[List[Union[float, None, str]]] def griddata( points: MatrixInput, values: MatrixInput, xi: MatrixInput, griddata_method: Union[str, ScalarInput] = "linear", fill_value: Optional[ScalarInput] = None, ) -> GriddataResult: """ Interpolate unstructured N-dimensional data using scipy.interpolate.griddata. Args: points: 2D list of coordinates for data points with consistent column count (shape: n_points x n_dim). values: 2D column vector of values at each point (shape: n_points x 1). xi: 2D list of coordinates where interpolation is desired (shape: n_xi x n_dim). griddata_method: Interpolation method; one of 'linear', 'nearest', or 'cubic'. fill_value: Value used for points outside the convex hull; defaults to SciPy behaviour when omitted. Returns: 2D column vector (list of lists) containing interpolated values or None for out-of-hull evaluations. This example function is provided as-is without any representation of accuracy. """ def _wrap_scalar_to_matrix(arg: MatrixInput) -> MatrixInput: if isinstance(arg, (int, float, bool, str)) or arg is None: return [[arg]] return arg def _validate_matrix(arg: MatrixInput, name: str) -> Optional[List[List[Any]]]: matrix_candidate = _wrap_scalar_to_matrix(arg) if not isinstance(matrix_candidate, list) or not matrix_candidate: return None if any(not isinstance(row, list) or not row for row in matrix_candidate): return None return matrix_candidate def _convert_matrix( matrix: List[List[Any]], name: str, expect_columns: Optional[int] = None, ) -> Union[List[List[float]], GriddataResult]: converted: List[List[float]] = [] for row in matrix: try: converted_row = [float(item) for item in row] except Exception: return [[f"Invalid input: {name} must contain numeric values."]] converted.append(converted_row) if expect_columns is not None and any(len(row) != expect_columns for row in converted): return [[f"Invalid input: {name} must have {expect_columns} columns."]] column_count = len(converted[0]) if any(len(row) != column_count for row in converted): return [[f"Invalid input: {name} must have consistent column counts."]] return converted def _extract_numeric_matrix(candidate: List[List[Any]]) -> Optional[List[List[float]]]: numeric_matrix: List[List[float]] = [] for row in candidate: numeric_row: List[float] = [] for item in row: if isinstance(item, str): return None try: numeric_row.append(float(item)) except Exception: return None numeric_matrix.append(numeric_row) return numeric_matrix prepared_points = _validate_matrix(points, "points") if prepared_points is None: return [["Invalid input: points must be a 2D list with no empty rows."]] prepared_xi = _validate_matrix(xi, "xi") if prepared_xi is None: return [["Invalid input: xi must be a 2D list with no empty rows."]] prepared_values = _validate_matrix(values, "values") if prepared_values is None: return [["Invalid input: values must be a 2D list with no empty rows."]] points_matrix_or_error = _convert_matrix(prepared_points, "points") if not isinstance(points_matrix_or_error, list) or not points_matrix_or_error or not isinstance(points_matrix_or_error[0], list): return points_matrix_or_error # type: ignore[return-value] extracted_points = _extract_numeric_matrix(points_matrix_or_error) if extracted_points is None: return points_matrix_or_error # type: ignore[return-value] points_matrix = extracted_points xi_matrix_or_error = _convert_matrix(prepared_xi, "xi", expect_columns=len(points_matrix[0])) if not isinstance(xi_matrix_or_error, list) or not xi_matrix_or_error or not isinstance(xi_matrix_or_error[0], list): return xi_matrix_or_error # type: ignore[return-value] extracted_xi = _extract_numeric_matrix(xi_matrix_or_error) if extracted_xi is None: return xi_matrix_or_error # type: ignore[return-value] xi_matrix = extracted_xi values_matrix_or_error = _convert_matrix(prepared_values, "values", expect_columns=1) if not isinstance(values_matrix_or_error, list) or not values_matrix_or_error or not isinstance(values_matrix_or_error[0], list): return values_matrix_or_error # type: ignore[return-value] extracted_values = _extract_numeric_matrix(values_matrix_or_error) if extracted_values is None: return values_matrix_or_error # type: ignore[return-value] values_matrix = extracted_values if len(values_matrix) != len(points_matrix): return [["Invalid input: values must have the same number of rows as points."]] points_array = np.array(points_matrix, dtype=float) values_array = np.array(values_matrix, dtype=float).reshape(-1) xi_array = np.array(xi_matrix, dtype=float) if points_array.ndim != 2 or xi_array.ndim != 2: return [["Invalid input: points and xi must be 2D lists."]] if points_array.shape[1] != xi_array.shape[1]: return [["Invalid input: points and xi must have the same number of columns."]] method_candidate = griddata_method if isinstance(method_candidate, (int, float, bool)): method_candidate = str(method_candidate) if not isinstance(method_candidate, str): return [["Invalid input: griddata_method must be a string identifier."]] method_value = method_candidate.lower().strip() if method_value not in {"linear", "nearest", "cubic"}: return [["Invalid input: griddata_method must be 'linear', 'nearest', or 'cubic'."]] fill_value_processed: Optional[float] if fill_value is None: fill_value_processed = None else: try: fill_value_processed = float(fill_value) except Exception: return [["Invalid input: fill_value must be numeric or omitted."]] try: # Use SciPy's default NaN fill when a fill_value is not explicitly provided. if fill_value_processed is None: interpolated = scipy_griddata(points_array, values_array, xi_array, method=method_value) else: interpolated = scipy_griddata( points_array, values_array, xi_array, method=method_value, fill_value=fill_value_processed, ) except Exception as exc: return [[f"scipy.interpolate.griddata error: {exc}"]] interpolated_array = np.asarray(interpolated) if interpolated_array.ndim == 0: flattened_results = [float(interpolated_array)] else: flattened_results = interpolated_array.reshape(-1).tolist() output: GriddataResult = [] for item in flattened_results: try: numeric_value = float(item) except Exception: return [[f"Invalid output from scipy.interpolate.griddata: {item}"]] if np.isnan(numeric_value) or np.isinf(numeric_value): output.append([None]) else: output.append([numeric_value]) return output

Example Workbook

Link to Workbook 

Last updated on