Dataclass for storing intermediate values for GroupBy operation. Returned by the ``factorize`` method on Grouper objects. Attributes ---------- codes : DataArray Same shape as the DataArray to group by. Values consist of a unique integer code for each group. full_in
| 76 | |
| 77 | @dataclass(init=False) |
| 78 | class EncodedGroups: |
| 79 | """ |
| 80 | Dataclass for storing intermediate values for GroupBy operation. |
| 81 | Returned by the ``factorize`` method on Grouper objects. |
| 82 | |
| 83 | Attributes |
| 84 | ---------- |
| 85 | codes : DataArray |
| 86 | Same shape as the DataArray to group by. Values consist of a unique integer code for each group. |
| 87 | full_index : pd.Index |
| 88 | Pandas Index for the group coordinate containing unique group labels. |
| 89 | This can differ from ``unique_coord`` in the case of resampling and binning, |
| 90 | where certain groups in the output need not be present in the input. |
| 91 | group_indices : tuple of int or slice or list of int, optional |
| 92 | List of indices of array elements belonging to each group. Inferred if not provided. |
| 93 | unique_coord : Variable, optional |
| 94 | Unique group values present in dataset. Inferred if not provided |
| 95 | """ |
| 96 | |
| 97 | codes: DataArray |
| 98 | full_index: pd.Index |
| 99 | group_indices: GroupIndices = field(init=False, repr=False) |
| 100 | unique_coord: Variable | _DummyGroup = field(init=False, repr=False) |
| 101 | coords: Coordinates = field(init=False, repr=False) |
| 102 | |
| 103 | def __init__( |
| 104 | self, |
| 105 | codes: DataArray, |
| 106 | full_index: pd.Index, |
| 107 | group_indices: GroupIndices | None = None, |
| 108 | unique_coord: Variable | _DummyGroup | None = None, |
| 109 | coords: Coordinates | None = None, |
| 110 | ): |
| 111 | from xarray.core.groupby import _codes_to_group_indices |
| 112 | |
| 113 | assert isinstance(codes, DataArray) |
| 114 | if codes.name is None: |
| 115 | raise ValueError("Please set a name on the array you are grouping by.") |
| 116 | self.codes = codes |
| 117 | assert isinstance(full_index, pd.Index) |
| 118 | self.full_index = full_index |
| 119 | |
| 120 | if group_indices is None: |
| 121 | if not is_chunked_array(codes.data): |
| 122 | self.group_indices = tuple( |
| 123 | g |
| 124 | for g in _codes_to_group_indices( |
| 125 | codes.data.ravel(), len(full_index) |
| 126 | ) |
| 127 | if g |
| 128 | ) |
| 129 | else: |
| 130 | # We will not use this when grouping by a chunked array |
| 131 | self.group_indices = tuple() |
| 132 | else: |
| 133 | self.group_indices = group_indices |
| 134 | |
| 135 | if unique_coord is None: |
no outgoing calls
searching dependent graphs…