Skip to content

stats(return_type=...) is not validated; bad values silently return a raw numpy array #2558

@brendancol

Description

@brendancol

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

  1. Validate return_type at entry to stats() against an allowed set.
  2. Raise a clear ValueError listing the allowed values.
  3. Expand the docstring to enumerate what each allowed value returns.
  4. Add tests covering both valid values, the default, and the invalid case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions