Skip to content

Commit

Permalink
Merge pull request #54 from Badboy-16/14
Browse files Browse the repository at this point in the history
Check that input args are xarray DataArrays
  • Loading branch information
dougiesquire authored May 9, 2021
2 parents e95d96a + 5d7bfd7 commit daf41f0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ v0.2.1 (not yet released)
xhistogram that mimic the numpy histogram API. This included
adding a range argument to the xhistogram API :issue:`13`.
By `Dougie Squire <https://github.com/dougiesquire>`_.
- Added a function to check if the object passed to xhist() is an
xarray.DataArray and if not, throw an error. :issue:`14`.
By `Yang Yunyi <https://github.com/Badboy-16>`_.

v0.2.0
~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions xhistogram/test/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,10 @@ def test_carry_coords(keep_coords, number_of_inputs):
assert "lon" in result.coords
else:
assert "lon" not in result.coords


# test for issue #14
def test_input_type_check():
np_array = np.arange(100)
with pytest.raises(TypeError):
histogram(np_array)
12 changes: 9 additions & 3 deletions xhistogram/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def histogram(
density=False,
block_size="auto",
keep_coords=False,
bin_dim_suffix="_bin"
bin_dim_suffix="_bin",
):
"""Histogram applied along specified dimensions.
Expand Down Expand Up @@ -106,7 +106,13 @@ def histogram(
N_weights = 1 if weights is not None else 0

for a in args:
# TODO: make this a more robust check
if not isinstance(a, xr.DataArray):
raise TypeError(
"xhistogram.xarray.histogram accepts only xarray.DataArray "
+ f"objects but a {type(a).__name__} was provided"
)

for a in args:
assert a.name is not None, "all arrays must have a name"

# we drop coords to simplify alignment
Expand Down Expand Up @@ -161,7 +167,7 @@ def histogram(
range=range,
axis=axis,
density=density,
block_size=block_size
block_size=block_size,
)

# create output dims
Expand Down

0 comments on commit daf41f0

Please sign in to comment.