Skip to content

Add SeasonGrouper, SeasonResampler #9524

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

Merged
merged 44 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7e3a6a4
Add SeasonGrouper, SeasonResampler
dcherian Jun 28, 2024
879b496
Allow sliding seasons
dcherian Sep 20, 2024
8268c46
cftime support
dcherian Sep 22, 2024
31cc519
Add skeleton tests
dcherian Sep 22, 2024
96ae241
Support "subsampled" seasons
dcherian Sep 22, 2024
77dc5e0
small edits
dcherian Sep 22, 2024
d68b1e4
Add reset
dcherian Nov 12, 2024
1b7a9fc
Fix tests
dcherian Nov 14, 2024
be5f933
Raise if seasons are not sorted for resampling
dcherian Nov 14, 2024
bd21b48
fix Self import
dcherian Nov 14, 2024
09640b7
Redo calendar fixtures
dcherian Nov 14, 2024
8773faf
fix test
dcherian Nov 14, 2024
879af59
cftime tests
dcherian Nov 15, 2024
2ca67da
Fix doctest
dcherian Nov 16, 2024
f5191e5
typing
dcherian Nov 16, 2024
2512d53
fix test
dcherian Nov 16, 2024
f0f838c
Merge branch 'main' into custom-groupers
dcherian Nov 16, 2024
b9507fe
Merge branch 'main' into custom-groupers
dcherian Nov 16, 2024
b385532
Add tests for SeasonGrouper API (PR #9524) (#40)
tomvothecoder Nov 20, 2024
a21952a
try fixing test
dcherian Nov 21, 2024
9f3c270
Merge branch 'main' into custom-groupers
dcherian Jan 8, 2025
bc86751
lint
dcherian Jan 8, 2025
a62628b
Merge branch 'main' into custom-groupers
dcherian Mar 19, 2025
64c99c5
format
dcherian Mar 19, 2025
594f285
fix test
dcherian Mar 19, 2025
1313ab9
cleanup
dcherian Mar 19, 2025
32d9ed0
more cleanup
dcherian Mar 19, 2025
b068e94
fix
dcherian Mar 19, 2025
b9a34ca
Merge branch 'main' into custom-groupers
dcherian Mar 20, 2025
862cf2a
Fix automatic inference of unique_coord
dcherian Mar 20, 2025
f3f7d52
Squashed commit of the following:
dcherian Mar 20, 2025
85d9217
cleanup
dcherian Mar 20, 2025
de26f38
Fix
dcherian Mar 20, 2025
fc7297a
fix docstring
dcherian Mar 20, 2025
e3413f3
Merge remote-tracking branch 'upstream/main' into custom-groupers
dcherian Mar 25, 2025
861da6c
cleanup
dcherian Mar 26, 2025
7406458
Avoid silly sphinx complete rebuilds
dcherian Mar 26, 2025
6297c1c
Add docs
dcherian Mar 26, 2025
0d8210a
Merge branch 'main' into custom-groupers
dcherian Apr 2, 2025
6e6e55a
Merge branch 'main' into custom-groupers
dcherian Apr 8, 2025
db80db4
Merge branch 'main' into custom-groupers
dcherian Apr 29, 2025
6dbf8c9
Merge branch 'main' into custom-groupers
dcherian May 2, 2025
9786ed5
Merge branch 'main' into custom-groupers
dcherian May 6, 2025
3ee3fde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 6, 2025
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
Prev Previous commit
Next Next commit
Raise if seasons are not sorted for resampling
  • Loading branch information
dcherian committed Nov 14, 2024
commit be5f9337fede955ab8cbc9fdaeb077e3cb643549
32 changes: 32 additions & 0 deletions xarray/groupers.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,32 @@ def inds_to_string(asints: tuple[tuple[int, ...], ...]) -> tuple[str, ...]:
return tuple("".join([inits[i_ - 1] for i_ in t]) for t in asints)


def is_sorted_periodic(lst):
n = len(lst)

# Find the wraparound point where the list decreases
wrap_point = -1
for i in range(1, n):
if lst[i] < lst[i - 1]:
wrap_point = i
break

# If no wraparound point is found, the list is already sorted
if wrap_point == -1:
return True

# Check if both parts around the wrap point are sorted
for i in range(1, wrap_point):
if lst[i] < lst[i - 1]:
return False
for i in range(wrap_point + 1, n):
if lst[i] < lst[i - 1]:
return False

# Check wraparound condition
return lst[-1] <= lst[0]


@dataclass
class SeasonsGroup:
seasons: tuple[str, ...]
Expand Down Expand Up @@ -749,6 +775,12 @@ def __post_init__(self):
)
self.season_tuples = dict(zip(self.seasons, self.season_inds, strict=True))

if not is_sorted_periodic(list(itertools.chain(*self.season_inds))):
raise ValueError(
"Resampling is only supported with sorted seasons. "
f"Provided seasons {self.seasons!r} are not sorted."
)

def factorize(self, group: T_Group) -> EncodedGroups:
if group.ndim != 1:
raise ValueError(
Expand Down
9 changes: 8 additions & 1 deletion xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,14 @@ def test_season_grouper_simple(self, calendar) -> None:
).mean()
assert_identical(expected, actual)

@pytest.mark.parametrize("seasons", [["JJA", "MAM", "SON", "DJF"]])
def test_season_resampling_raises_unsorted_seasons(self, seasons):
calendar = "standard"
time = date_range("2001-01-01", "2002-12-30", freq="D", calendar=calendar)
da = DataArray(np.ones(time.size), dims="time", coords={"time": time})
with pytest.raises(ValueError, match="sort"):
da.resample(time=SeasonResampler(seasons))

# TODO: drop_incomplete
@requires_cftime
@pytest.mark.parametrize("drop_incomplete", [True, False])
Expand All @@ -3186,7 +3194,6 @@ def test_season_grouper_simple(self, calendar) -> None:
pytest.param(["DJF", "MAM", "JJA", "ON"], id="skip-september"),
pytest.param(["JJAS"], id="jjas-only"),
pytest.param(["MAM", "JJA", "SON", "DJF"], id="different-order"),
pytest.param(["JJA", "MAM", "SON", "DJF"], id="out-of-order"),
],
)
def test_season_resampler(self, seasons: list[str], drop_incomplete: bool) -> None:
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy