Skip to content

Commit

Permalink
Add clear error message when no complete seasons are found
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvothecoder committed Jul 30, 2024
1 parent 45b5cb0 commit f6858ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 15 additions & 0 deletions tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,21 @@ def test_raises_error_with_incorrect_custom_seasons_argument(self):
season_config={"custom_seasons": custom_seasons},
)

def test_raises_error_with_dataset_that_has_no_complete_seasons(self):
ds = self.ds.copy()
ds = ds.isel(time=slice(0, 1))
custom_seasons = [["Dec", "Jan"]]

with pytest.raises(RuntimeError):
ds.temporal.group_average(
"ts",
"season",
season_config={
"custom_seasons": custom_seasons,
"drop_incomplete_seasons": True,
},
)

def test_weighted_custom_seasonal_averages(self):
ds = self.ds.copy()

Expand Down
11 changes: 9 additions & 2 deletions xcdat/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def group_average(
Time bounds are used for generating weights to calculate weighted group
averages (refer to the ``weighted`` parameter documentation below).
.. deprecated:: v0.7.0
.. deprecated:: v0.8.0
The ``season_config`` dictionary argument ``"drop_incomplete_djf"``
is being deprecated. Please use ``"drop_incomplete_seasons"``
instead.
Expand Down Expand Up @@ -1207,7 +1207,14 @@ def _drop_incomplete_seasons(self, ds: xr.Dataset) -> xr.Dataset:
# Get the incomplete seasons and drop the time coordinates that are in
# those incomplete seasons.
indexes_to_drop = df[df["expected_months"] != df["actual_months"]].index
if len(indexes_to_drop) > 0:

if len(indexes_to_drop) == len(time_coords):
raise RuntimeError(
"No time coordinates remain with `drop_incomplete_seasons=True`. "
"Check the dataset has at least one complete season and/or "
"specify `drop_incomplete_seasons=False` instead."
)
elif len(indexes_to_drop) > 0:
# The dataset needs to be split into a dataset with and a dataset
# without the time dimension because the xarray `.where()` method
# concatenates the time dimension to non-time dimension data vars,
Expand Down

0 comments on commit f6858ff

Please sign in to comment.