Skip to content

Commit f0b7eb5

Browse files
authored
FlagGrouper: Encapsulate instead of subclassing UniqueGrouper (#569)
1 parent dc80226 commit f0b7eb5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

cf_xarray/groupers.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import numpy as np
44
import pandas as pd
5-
from xarray.groupers import EncodedGroups, UniqueGrouper
5+
from xarray import Variable
6+
from xarray.groupers import EncodedGroups, Grouper, UniqueGrouper
67

78

89
@dataclass
9-
class FlagGrouper(UniqueGrouper):
10+
class FlagGrouper(Grouper):
1011
"""
1112
Grouper object that allows convenient categorical grouping by a CF flag variable.
1213
@@ -23,18 +24,24 @@ def factorize(self, group) -> EncodedGroups:
2324
values = np.array(group.attrs["flag_values"])
2425
full_index = pd.Index(group.attrs["flag_meanings"].split(" "))
2526

26-
self.labels = values
27+
grouper = UniqueGrouper(labels=values)
2728

2829
# TODO: we could optimize here, since `group` is already factorized,
2930
# but there are subtleties. For example, the attrs must be up to date,
3031
# any value that is not in flag_values will cause an error, etc.
31-
ret = super().factorize(group)
32+
ret = grouper.factorize(group)
3233

3334
ret.codes.attrs.pop("flag_values")
3435
ret.codes.attrs.pop("flag_meanings")
3536

3637
return EncodedGroups(
3738
codes=ret.codes,
3839
full_index=full_index,
40+
unique_coord=Variable(
41+
dims=ret.codes.name, data=np.array(full_index), attrs=ret.codes.attrs
42+
),
3943
group_indices=ret.group_indices,
4044
)
45+
46+
def reset(self):
47+
raise NotImplementedError()

0 commit comments

Comments
 (0)