xrspatial.zonal.stats(zones: xarray.core.dataarray.DataArray, values: xarray.core.dataarray.DataArray, stat_funcs=['mean', 'max', 'min', 'std', 'var', 'count'])[source]

Calculate summary statistics for each zone defined by a zone dataset, based on values aggregate.

A single output value is computed for every zone in the input zone dataset.

zones: xarray.DataArray,

zones.values is a 2d array of integers. A zone is all the cells in a raster that have the same value, whether or not they are contiguous. The input zone layer defines the shape, values, and locations of the zones. An integer field in the zone input is specified to define the zones.

values: xarray.DataArray,

values.values is a 2d array of integers or floats. The input value raster contains the input values used in calculating the output statistic for each zone.

stat_funcs: list of strings or dictionary<stat_name: func(zone_values)>.

(default = [‘mean’, ‘max’, ‘min’, ‘std’, ‘var’, ‘count’])

Which statistics to calculate for each zone. If a list, possible choices are subsets of [‘mean’, ‘max’, ‘min’, ‘std’, ‘var’, ‘count’] In the dictionary case, all of its values must be callable. Function takes only one argument that is the zone values. The key become the column name in the output DataFrame.

stats_df: pandas.DataFrame

A pandas DataFrame where each column is a statistic and each row is a zone with zone id.

>>> zones_val = np.array([[1, 1, 0, 2],
>>>                      [0, 2, 1, 2]])
>>> zones = xarray.DataArray(zones_val)
>>> values_val = np.array([[2, -1, 5, 3],
>>>                       [3, np.nan, 20, 10]])
>>> values = xarray.DataArray(values_val)

# default setting >>> df = stats(zones, values) >>> df

mean max min std var

1 7.0 20.0 -1.0 9.273618 86.00 2 6.5 10.0 3.0 3.500000 12.25

# custom stat >>> custom_stats ={‘sum’: lambda val: val.sum()} >>> df = stats(zones, values, stat_funcs=custom_stats) >>> df


1 21.0 2 13.0