Repository: xgcm/xhistogram

Issue:
error in keep_coords option in xarray histogram? 
Thanks to the developers of this fabulous tool!  I'm getting the following error when using histogram from xhistogram.xarray with keep_coords==True:

TypeError: can only concatenate tuple (not "list") to tuple

I'm sending in a single xarray as args and want to retain the coordinates from my weights xarray. The error occurs at line 122 of xarray.py--since args is not a list, the += fails. I'm still a python newbie and don't know what the fix should be (maybe args=[args] in the line above?), but thought I'd bring it to your attention.  Thanks again. 

Discussion:

Patch:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 0759ac7..5425895 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,6 +1,6 @@
 repos:
 -   repo: https://github.com/psf/black
-    rev: 20.8b1
+    rev: 22.3.0
     hooks:
     - id: black
       language_version: python3
diff --git a/doc/contributing.rst b/doc/contributing.rst
index e05fe0c..9ccbb9e 100644
--- a/doc/contributing.rst
+++ b/doc/contributing.rst
@@ -140,6 +140,21 @@ Preparing Pull Requests
 Release History
 ---------------
 
+v0.3.2 (not released)
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+- Fix bug producing TypeError when weights is provided with
+  `keep_coords=True` :issue:`78`. By
+  `Dougie Squire <https://github.com/dougiesquire>`_.
+- Raise TypeError when weights is a dask array and bin edges are
+  not explicitly provided :issue:`12`. By
+  `Dougie Squire <https://github.com/dougiesquire>`_.
+
+v0.3.1
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+- Add DOI badge and CITATION.cff. By
+  `Julius Busecke <https://github.com/jbusecke>`_.
 
 v0.3.0
 ~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/doc/environment.yml b/doc/environment.yml
index 685694f..4691ee3 100644
--- a/doc/environment.yml
+++ b/doc/environment.yml
@@ -5,7 +5,7 @@ dependencies:
   - python=3.9
   - xarray
   - netcdf4
-  - numpy>=1.16
+  - numpy>=1.16,!=1.20.0,!=1.20.1,!=1.20.2,!=1.20.3
   - pytest
   - numpydoc
   - sphinx
diff --git a/xhistogram/xarray.py b/xhistogram/xarray.py
index 922fc26..f1b7976 100644
--- a/xhistogram/xarray.py
+++ b/xhistogram/xarray.py
@@ -100,6 +100,7 @@ def histogram(
 
     """
 
+    args = list(args)
     N_args = len(args)
 
     # TODO: allow list of weights as well


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 3b13363..1bd0551 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -1,6 +1,6 @@
 name: Tests
 
-on: [push, pull_request]
+on: [push, pull_request, workflow_dispatch]
 
 jobs:
   test:
diff --git a/xhistogram/test/test_xarray.py b/xhistogram/test/test_xarray.py
index 7170537..89a8f56 100644
--- a/xhistogram/test/test_xarray.py
+++ b/xhistogram/test/test_xarray.py
@@ -175,24 +175,34 @@ def test_dims_and_coords():
 
 @pytest.mark.parametrize("number_of_inputs", [1, 2])
 @pytest.mark.parametrize("keep_coords", [True, False])
-def test_carry_coords(keep_coords, number_of_inputs):
+@pytest.mark.parametrize("include_weights", [True, False])
+def test_carry_coords(keep_coords, number_of_inputs, include_weights):
     time_axis = np.arange(40)
     X_axis = np.arange(10)
     Y_axis = np.arange(10)
+    weight_value = 0.5
+
     data = np.random.randint(
         low=0, high=100, size=(len(time_axis), len(X_axis), len(Y_axis))
     )
     da = xr.DataArray(
         data, coords=[time_axis, X_axis, Y_axis], dims=["time", "X", "Y"], name="one"
     )
+
+    if include_weights:
+        weights = xr.full_like(da, weight_value)
+    else:
+        weights = None
+
     # faking coordinates
-    da["lon"] = da.X ** 2 + da.Y ** 2
+    da["lon"] = da.X**2 + da.Y**2
     assert "lon" in da.coords
     bins = np.linspace(0, 100, 10)
     result = histogram(
         *[da] * number_of_inputs,
         bins=[bins] * number_of_inputs,
         dim=["time"],
+        weights=weights,
         keep_coords=keep_coords
     )
     if keep_coords: