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

Class SeasonResampler

xarray/groupers.py:874–1109  ·  view source on GitHub ↗

Allows grouping using a custom definition of seasons. Parameters ---------- seasons: Sequence[str] An ordered list of seasons. drop_incomplete: bool Whether to drop seasons that are not completely included in the data. For example, if a time series starts in

Source from the content-addressed store, hash-verified

872
873@dataclass
874class SeasonResampler(Resampler):
875 """Allows grouping using a custom definition of seasons.
876
877 Parameters
878 ----------
879 seasons: Sequence[str]
880 An ordered list of seasons.
881 drop_incomplete: bool
882 Whether to drop seasons that are not completely included in the data.
883 For example, if a time series starts in Jan-2001, and seasons includes `"DJF"`
884 then observations from Jan-2001, and Feb-2001 are ignored in the grouping
885 since Dec-2000 isn't present.
886
887 Examples
888 --------
889 >>> SeasonResampler(["JF", "MAM", "JJAS", "OND"])
890 SeasonResampler(seasons=['JF', 'MAM', 'JJAS', 'OND'], drop_incomplete=True)
891
892 >>> SeasonResampler(["DJFM", "AM", "JJA", "SON"])
893 SeasonResampler(seasons=['DJFM', 'AM', 'JJA', 'SON'], drop_incomplete=True)
894 """
895
896 seasons: Sequence[str]
897 drop_incomplete: bool = field(default=True, kw_only=True)
898 season_inds: Sequence[Sequence[int]] = field(init=False, repr=False)
899 season_tuples: Mapping[str, Sequence[int]] = field(init=False, repr=False)
900
901 def __post_init__(self):
902 self.season_inds = season_to_month_tuple(self.seasons)
903 all_inds = functools.reduce(operator.add, self.season_inds)
904 if len(all_inds) > len(set(all_inds)):
905 raise ValueError(
906 f"Overlapping seasons are not allowed. Received {self.seasons!r}"
907 )
908 self.season_tuples = dict(zip(self.seasons, self.season_inds, strict=True))
909
910 if not is_sorted_periodic(list(itertools.chain(*self.season_inds))):
911 raise ValueError(
912 "Resampling is only supported with sorted seasons. "
913 f"Provided seasons {self.seasons!r} are not sorted."
914 )
915
916 def factorize(self, group: T_Group) -> EncodedGroups:
917 if group.ndim != 1:
918 raise ValueError(
919 "SeasonResampler can only be used to resample by 1D arrays."
920 )
921 if not isinstance(group, DataArray) or not _contains_datetime_like_objects(
922 group.variable
923 ):
924 raise ValueError(
925 "SeasonResampler can only be used to group by datetime-like DataArrays."
926 )
927
928 seasons = self.seasons
929 season_inds = self.season_inds
930 season_tuples = self.season_tuples
931

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…