Description
stats(return_type=...) in xrspatial/zonal.py silently accepts any string and falls through to an undocumented path. The dispatch is:
return_type == 'pandas.DataFrame' returns a pandas.DataFrame.
return_type == 'xarray.DataArray' wraps the buffer into an xr.DataArray.
- Anything else returns the raw
numpy.ndarray stats buffer from _stats_numpy.
So stats(zones, values, return_type='bogus') returns a numpy array of shape (n_stats, *values.shape) instead of raising. Typos in return_type are hidden, and callers get back a type that is not documented.
Expected behavior
return_type should be validated at entry. Only the documented values ('pandas.DataFrame', 'xarray.DataArray') should be accepted. Anything else should raise ValueError listing the allowed values.
Reproducer
import numpy as np, xarray as xr
from xrspatial.zonal import stats
zones = xr.DataArray(np.array([[0, 0], [1, 1]]))
values = xr.DataArray(np.array([[1.0, 2.0], [3.0, 4.0]]))
result = stats(zones, values, return_type='bogus')
print(type(result)) # numpy.ndarray -- should raise ValueError
Proposed fix
- Validate
return_type at entry to stats() against an allowed set.
- Raise a clear
ValueError listing the allowed values.
- Expand the docstring to enumerate what each allowed value returns.
- Add tests covering both valid values, the default, and the invalid case.
Description
stats(return_type=...)inxrspatial/zonal.pysilently accepts any string and falls through to an undocumented path. The dispatch is:return_type == 'pandas.DataFrame'returns apandas.DataFrame.return_type == 'xarray.DataArray'wraps the buffer into anxr.DataArray.numpy.ndarraystats buffer from_stats_numpy.So
stats(zones, values, return_type='bogus')returns a numpy array of shape(n_stats, *values.shape)instead of raising. Typos inreturn_typeare hidden, and callers get back a type that is not documented.Expected behavior
return_typeshould be validated at entry. Only the documented values ('pandas.DataFrame','xarray.DataArray') should be accepted. Anything else should raiseValueErrorlisting the allowed values.Reproducer
Proposed fix
return_typeat entry tostats()against an allowed set.ValueErrorlisting the allowed values.