From d0f139610e3c60cf6e2ab495445d3e0066f2164a Mon Sep 17 00:00:00 2001 From: dcherian Date: Tue, 24 Sep 2019 17:39:55 -0600 Subject: [PATCH 1/6] groupby repr --- xarray/core/groupby.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index bae3057aabe..3399b27b3e7 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -9,6 +9,7 @@ from .arithmetic import SupportsArithmetic from .common import ImplementsArrayReduce, ImplementsDatasetReduce from .concat import concat +from .formatting import format_array_flat from .options import _get_keep_attrs from .pycompat import integer_types from .utils import ( @@ -158,6 +159,15 @@ def ndim(self): def values(self): return range(self.size) + @property + def shape(self): + return (self.size,) + + def __getitem__(self, key): + if isinstance(key, tuple): + key = key[0] + return self.values[key] + def _ensure_1d(group, obj): if group.ndim != 1: @@ -383,6 +393,14 @@ def __len__(self): def __iter__(self): return zip(self._unique_coord.values, self._iter_grouped()) + def __repr__(self): + return "%s, grouped over %r \n%r groups with labels %s" % ( + self.__class__.__name__, + self._unique_coord.name, + self._unique_coord.size, + ", ".join(format_array_flat(self._unique_coord, 30).split()), + ) + def _get_index_and_items(self, index, grouper): from .resample_cftime import CFTimeGrouper From 3115127e4481ddc4fb5c9255a51ce7da5a83b060 Mon Sep 17 00:00:00 2001 From: dcherian Date: Wed, 25 Sep 2019 09:27:48 -0600 Subject: [PATCH 2/6] add test. --- xarray/tests/test_groupby.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index ee17cc39064..b4020027915 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -202,4 +202,28 @@ def test_da_groupby_assign_coords(): assert_identical(expected, actual2) +test_da = xr.DataArray( + np.random.randn(10, 20, 6), + dims=["x", "y", "z"], + coords={"z": ["a", "b", "c", "a", "b", "c"], "x": [1, 1, 1, 2, 2, 3, 4, 5, 3, 4]}, +) + + +@pytest.mark.parametrize("dim", ["x", "y", "z"]) +@pytest.mark.parametrize("obj", [test_da, test_da.to_dataset(name="a")]) +def test_groupby_repr(obj, dim): + + actual = repr(obj.groupby(dim)) + expected = "%sGroupBy" % obj.__class__.__name__ + expected += ", grouped over %r " % dim + expected += "\n%r groups with labels " % (len(np.unique(obj[dim]))) + if dim == "x": + expected += "1, 2, 3, 4, 5" + elif dim == "y": + expected += "0, 1, 2, 3, 4, 5, ..., 15, 16, 17, 18, 19" + elif dim == "z": + expected += "'a', 'b', 'c'" + assert actual == expected + + # TODO: move other groupby tests from test_dataset and test_dataarray over here From 6110b2fb8b67ae77426a520ec8a455ceafefba9f Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 26 Sep 2019 07:52:03 -0600 Subject: [PATCH 3/6] test datetime and nondim coord --- xarray/tests/test_groupby.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index b4020027915..bc59747bcbc 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -203,16 +203,20 @@ def test_da_groupby_assign_coords(): test_da = xr.DataArray( - np.random.randn(10, 20, 6), - dims=["x", "y", "z"], - coords={"z": ["a", "b", "c", "a", "b", "c"], "x": [1, 1, 1, 2, 2, 3, 4, 5, 3, 4]}, + np.random.randn(10, 20, 6, 24), + dims=["x", "y", "z", "t"], + coords={ + "z": ["a", "b", "c", "a", "b", "c"], + "x": [1, 1, 1, 2, 2, 3, 4, 5, 3, 4], + "t": pd.date_range("2001-01-01", freq="M", periods=24), + "month": ("t", list(range(1, 13)) * 2), + }, ) -@pytest.mark.parametrize("dim", ["x", "y", "z"]) +@pytest.mark.parametrize("dim", ["x", "y", "z", "month"]) @pytest.mark.parametrize("obj", [test_da, test_da.to_dataset(name="a")]) def test_groupby_repr(obj, dim): - actual = repr(obj.groupby(dim)) expected = "%sGroupBy" % obj.__class__.__name__ expected += ", grouped over %r " % dim @@ -223,6 +227,18 @@ def test_groupby_repr(obj, dim): expected += "0, 1, 2, 3, 4, 5, ..., 15, 16, 17, 18, 19" elif dim == "z": expected += "'a', 'b', 'c'" + elif dim == "month": + expected += "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12" + assert actual == expected + + +@pytest.mark.parametrize("obj", [test_da, test_da.to_dataset(name="a")]) +def test_groupby_repr_datetime(obj): + actual = repr(obj.groupby("t.month")) + expected = "%sGroupBy" % obj.__class__.__name__ + expected += ", grouped over 'month' " + expected += "\n%r groups with labels " % (len(np.unique(obj.t.dt.month))) + expected += "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12" assert actual == expected From dde70cf6cd4a4b9a88bb6a85b3834882b9f29a4e Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 26 Sep 2019 07:56:41 -0600 Subject: [PATCH 4/6] =?UTF-8?q?rename=20test=5Fda=20=E2=86=92=20repr=5Fda?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xarray/tests/test_groupby.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index bc59747bcbc..463a6a32185 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -202,7 +202,7 @@ def test_da_groupby_assign_coords(): assert_identical(expected, actual2) -test_da = xr.DataArray( +repr_da = xr.DataArray( np.random.randn(10, 20, 6, 24), dims=["x", "y", "z", "t"], coords={ @@ -215,7 +215,7 @@ def test_da_groupby_assign_coords(): @pytest.mark.parametrize("dim", ["x", "y", "z", "month"]) -@pytest.mark.parametrize("obj", [test_da, test_da.to_dataset(name="a")]) +@pytest.mark.parametrize("obj", [repr_da, repr_da.to_dataset(name="a")]) def test_groupby_repr(obj, dim): actual = repr(obj.groupby(dim)) expected = "%sGroupBy" % obj.__class__.__name__ @@ -232,7 +232,7 @@ def test_groupby_repr(obj, dim): assert actual == expected -@pytest.mark.parametrize("obj", [test_da, test_da.to_dataset(name="a")]) +@pytest.mark.parametrize("obj", [repr_da, repr_da.to_dataset(name="a")]) def test_groupby_repr_datetime(obj): actual = repr(obj.groupby("t.month")) expected = "%sGroupBy" % obj.__class__.__name__ From 5e574f0fdf98ffd9f9d2e539d0ba2daf20b95868 Mon Sep 17 00:00:00 2001 From: dcherian Date: Thu, 26 Sep 2019 08:49:11 -0600 Subject: [PATCH 5/6] Add whats-new --- doc/whats-new.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index d8d3382675e..db2c6b73ad7 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -18,6 +18,19 @@ What's New v0.13.1 (unreleased) -------------------- +New functions/methods +~~~~~~~~~~~~~~~~~~~~~ + +Enhancements +~~~~~~~~~~~~ + +- Add a repr for :py:class:`~xarray.core.GroupBy` objects. By `Deepak Cherian `_. + Example:: + + >>> da.groupby("time.season") + DataArrayGroupBy, grouped over 'season' + 4 groups with labels 'DJF', 'JJA', 'MAM', 'SON + Bug fixes ~~~~~~~~~ - Reintroduce support for :mod:`weakref` (broken in v0.13.0). Support has been From c53c15ff9a69a8f270ba904eac62270610515add Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Sat, 28 Sep 2019 16:50:14 +0000 Subject: [PATCH 6/6] Update doc/whats-new.rst --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1ac46d57f39..93fa93115c3 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -29,7 +29,7 @@ Enhancements >>> da.groupby("time.season") DataArrayGroupBy, grouped over 'season' - 4 groups with labels 'DJF', 'JJA', 'MAM', 'SON + 4 groups with labels 'DJF', 'JJA', 'MAM', 'SON' Bug fixes ~~~~~~~~~