Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groupby repr #3344

Merged
merged 9 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting._get_indexer_at_least_n_items returns a tuple so I unpack it here. This could alternatively be an if condition in formatting.first_n_items and formatting.last_n_items but I felt this might be cleaner. I guess we could isntead call np.array(self.values)[key]

if isinstance(key, tuple):
key = key[0]
return self.values[key]


def _ensure_1d(group, obj):
if group.ndim != 1:
Expand Down Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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