MCPcopy
hub / github.com/pydata/xarray / factorize

Method factorize

xarray/groupers.py:829–867  ·  view source on GitHub ↗
(self, group: T_Group)

Source from the content-addressed store, hash-verified

827 # drop_incomplete: bool = field(default=True) # TODO
828
829 def factorize(self, group: T_Group) -> EncodedGroups:
830 if TYPE_CHECKING:
831 assert not isinstance(group, _DummyGroup)
832 if not _contains_datetime_like_objects(group.variable):
833 raise ValueError(
834 "SeasonGrouper can only be used to group by datetime-like arrays."
835 )
836 months = group.dt.month.data
837 seasons_groups = find_independent_seasons(self.seasons)
838 codes_ = np.full((len(seasons_groups),) + group.shape, -1, dtype=np.int8)
839 group_indices: list[list[int]] = [[]] * len(self.seasons)
840 for axis_index, seasgroup in enumerate(seasons_groups):
841 for season_tuple, code in zip(
842 seasgroup.inds, seasgroup.codes, strict=False
843 ):
844 mask = np.isin(months, season_tuple)
845 codes_[axis_index, mask] = code
846 (indices,) = mask.nonzero()
847 group_indices[code] = indices.tolist()
848
849 if np.all(codes_ == -1):
850 raise ValueError(
851 "Failed to group data. Are you grouping by a variable that is all NaN?"
852 )
853 needs_dummy_dim = len(seasons_groups) > 1
854 codes = DataArray(
855 dims=(("__season_dim__",) if needs_dummy_dim else tuple()) + group.dims,
856 data=codes_ if needs_dummy_dim else codes_.squeeze(),
857 attrs=group.attrs,
858 name="season",
859 )
860 unique_coord = Variable("season", self.seasons, attrs=group.attrs)
861 full_index = pd.Index(self.seasons)
862 return EncodedGroups(
863 codes=codes,
864 group_indices=tuple(group_indices),
865 unique_coord=unique_coord,
866 full_index=full_index,
867 )
868
869 def reset(self) -> Self:
870 return type(self)(self.seasons)

Callers

nothing calls this directly

Calls 8

DataArrayClass · 0.90
VariableClass · 0.90
find_independent_seasonsFunction · 0.85
EncodedGroupsClass · 0.85
isinMethod · 0.80
allMethod · 0.45
squeezeMethod · 0.45

Tested by

no test coverage detected