From f6858ff8dd2d7e17114b66e85642bfdbc22f87bc Mon Sep 17 00:00:00 2001 From: tomvothecoder Date: Tue, 30 Jul 2024 12:30:32 -0700 Subject: [PATCH] Add clear error message when no complete seasons are found --- tests/test_temporal.py | 15 +++++++++++++++ xcdat/temporal.py | 11 +++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/test_temporal.py b/tests/test_temporal.py index fe8a2797..34658e1e 100644 --- a/tests/test_temporal.py +++ b/tests/test_temporal.py @@ -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() diff --git a/xcdat/temporal.py b/xcdat/temporal.py index d492d4ae..41c329bf 100644 --- a/xcdat/temporal.py +++ b/xcdat/temporal.py @@ -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. @@ -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,