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
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
cftime support
  • Loading branch information
dcherian committed Nov 12, 2024
commit 8268c468537bb04d490209aac7589e04606311d7
26 changes: 20 additions & 6 deletions xarray/groupers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@
from numpy.typing import ArrayLike

from xarray.coding.cftime_offsets import BaseCFTimeOffset, _new_to_legacy_freq
from xarray.coding.cftimeindex import CFTimeIndex
from xarray.core import duck_array_ops
from xarray.core.computation import apply_ufunc
from xarray.core.coordinates import Coordinates, _coordinates_from_variable
from xarray.core.coordinates import Coordinates
from xarray.core.common import _contains_datetime_like_objects
from xarray.core.common import _contains_datetime_like_objects
from xarray.core.common import (
_contains_cftime_datetimes,
_contains_datetime_like_objects,
)
from xarray.core.coordinates import Coordinates
from xarray.core.dataarray import DataArray
from xarray.core.duck_array_ops import isnull
from xarray.core.formatting import first_n_items
from xarray.core.groupby import T_Group, _DummyGroup
from xarray.core.indexes import safe_cast_to_index
from xarray.core.resample_cftime import CFTimeGrouper
Expand Down Expand Up @@ -567,7 +574,7 @@ def season_to_month_tuple(seasons: Sequence[str]) -> tuple[tuple[int, ...], ...]
initials = "JFMAMJJASOND"
starts = dict(
("".join(s), i + 1)
for s, i in zip(sliding_window(2, initials + "J"), range(12), strict=False)
for s, i in zip(sliding_window(2, initials + "J"), range(12), strict=True)
)
result: list[tuple[int, ...]] = []
for i, season in enumerate(seasons):
Expand Down Expand Up @@ -757,7 +764,7 @@ def factorize(self, group):
season_label = np.full(group.shape, "", dtype=f"U{nstr}")

# offset years for seasons with December and January
for season_str, season_ind in zip(seasons, season_inds, strict=False):
for season_str, season_ind in zip(seasons, season_inds, strict=True):
season_label[month.isin(season_ind)] = season_str
if "DJ" in season_str:
after_dec = season_ind[season_str.index("D") + 1 :]
Expand All @@ -775,10 +782,17 @@ def factorize(self, group):
first_items = g.first()
counts = g.count()

if _contains_cftime_datetimes(group.data):
index_class = CFTimeIndex
datetime_class = type(first_n_items(group.data, 1).item())
else:
index_class = pd.DatetimeIndex
datetime_class = datetime.datetime

# these are the seasons that are present
unique_coord = pd.DatetimeIndex(
unique_coord = index_class(
[
pd.Timestamp(year=year, month=season_tuples[season][0], day=1)
datetime_class(year=year, month=season_tuples[season][0], day=1)
for year, season in first_items.index
]
)
Expand Down Expand Up @@ -806,12 +820,12 @@ def factorize(self, group):
unique_codes[idx] = -1

# all years and seasons
complete_index = pd.DatetimeIndex(
complete_index = index_class(
# This sorted call is a hack. It's hard to figure out how
# to start the iteration
sorted(
[
pd.Timestamp(f"{y}-{m}-01")
datetime_class(year=y, month=m, day=1)
for y, m in itertools.product(
range(year[0].item(), year[-1].item() + 1),
[s[0] for s in season_inds],
Expand Down
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