diff --git a/CHANGES.rst b/CHANGES.rst index 10ef59099..d9454043d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,7 +8,7 @@ Contributors to this version: Juliette Lavoie (:user:`juliettelavoie`), Pascal B Announcements ^^^^^^^^^^^^^ -* To circumvent issues stemming from changes to the frequency code convention in `pandas` v2.2, we have pinned `xarray` (<2023.11.0) and `pandas` (< 2.2) for this release. This change will be reverted in `xclim` v0.48.0 to support the newer versions (`xarray >= 2023.11.0` and `pandas >= 2.2`). +* To circumvent issues stemming from changes to the frequency code convention in `pandas` v2.2, we have pinned `xarray` (< 2023.11.0) and `pandas` (< 2.2) for this release. This change will be reverted in `xclim` v0.48.0 to support the newer versions (`xarray>= 2023.11.0` and `pandas>= 2.2`). * `xclim` v0.47.0 will be the last release supporting Python3.8. New features and enhancements @@ -18,9 +18,10 @@ New features and enhancements Bug fixes ^^^^^^^^^ -* Fixed a bug with ``n_escore=-1`` in ``xclim.sdba.adjustment.NpdfTransform``. (:issue:`1515`, :pull:`1515`). -* In the documentation, fix the tooltips in the indicator search results. (:issue:`1524`, :pull:`1527`). +* Fixed a bug with ``n_escore=-1`` in ``xclim.sdba.adjustment.NpdfTransform``. (:issue:`1515`, :pull:`1516`). +* In the documentation, fixed the tooltips in the indicator search results. (:issue:`1524`, :pull:`1527`). * If chunked inputs are passed to indicators ``mean_radiant_temperature`` and ``potential_evapotranspiration``, sub-calculations of the solar angle will also use the same chunks, instead of a single one of the same size as the data. (:issue:`1536`, :pull:`1542`). +* Fix wrong attributes in ``xclim.indices.standardized_precipitation_index``, ``xclim.indices.standardized_precipitation_evapotranspiration_index``. (:issue:`1537`, :pull:`1538`). Internal changes ^^^^^^^^^^^^^^^^ diff --git a/tests/test_indices.py b/tests/test_indices.py index 77989b7b8..8f0b14e4d 100644 --- a/tests/test_indices.py +++ b/tests/test_indices.py @@ -653,6 +653,7 @@ def test_standardized_index_modularity(self, open_dataset): dist=dist, method=method, offset="1 mm/d", + month=[2, 3], ) spei1 = xci.standardized_precipitation_evapotranspiration_index( wb.sel(time=slice("1998", "2000")), params=params @@ -667,6 +668,7 @@ def test_standardized_index_modularity(self, open_dataset): offset="1 mm/d", cal_start="1950", cal_end="1980", + month=[2, 3], ).sel(time=slice("1998", "2000")) # In the previous computation, the first {window-1} values are NaN because the rolling is performed on the period [1998,2000]. diff --git a/xclim/indices/_agro.py b/xclim/indices/_agro.py index af70b63a4..f8571f669 100644 --- a/xclim/indices/_agro.py +++ b/xclim/indices/_agro.py @@ -1174,6 +1174,7 @@ def standardized_precipitation_index( ... cal_end=cal_end, ... ) # Computing SPI-3 months using a gamma distribution for the fit >>> # Fitting parameters can also be obtained ... + >>> from xclim.indices.stats import standardized_index_fit_params >>> params = standardized_index_fit_params( ... pr.sel(time=slice(cal_start, cal_end)), ... freq="MS", @@ -1189,7 +1190,12 @@ def standardized_precipitation_index( :cite:cts:`mckee_relationship_1993` """ if params is not None and pr_cal is None: - freq, window = (params.attrs[s] for s in ["freq", "window"]) + freq, window, indexer = ( + params.attrs[s] for s in ["freq", "window", "time_indexer"] + ) + # Unpack attrs to None and {} if needed + freq = None if freq == "" else freq + indexer = {} if indexer[0] == "" else {indexer[0]: indexer[1:]} if cal_start or cal_end: warnings.warn( "Expected either `cal_{start|end}` or `params`, got both. The `params` input overrides other inputs." @@ -1230,8 +1236,9 @@ def standardized_precipitation_index( if paramsd != template.sizes: params = params.broadcast_like(template) - spi = standardized_index(pr, params, **indexer) + spi = standardized_index(pr, params) spi.attrs = params.attrs + spi.attrs["freq"] = freq or xarray.infer_freq(spi.time) spi.attrs["units"] = "" return spi @@ -1324,7 +1331,7 @@ def standardized_precipitation_evapotranspiration_index( "Proceeding with the value given in `params`." ) offset = params_offset - offset = 0 if offset is None else convert_units_to(offset, wb, context="hydro") + offset = 0 if offset == "" else convert_units_to(offset, wb, context="hydro") # Allowed distributions are constrained by the SPI function if dist in ["gamma", "fisk"] and offset <= 0: raise ValueError( diff --git a/xclim/indices/stats.py b/xclim/indices/stats.py index 2fe52b7e9..8325ba623 100644 --- a/xclim/indices/stats.py +++ b/xclim/indices/stats.py @@ -724,15 +724,20 @@ def standardized_index_fit_params( ) params.attrs = { "calibration_period": cal_range, - "freq": freq, + "freq": freq or "", "window": window, "scipy_dist": dist, "method": method, "group": group, - "time_indexer": indexer, "units": "", - "offset": offset, + "offset": offset or "", } + if indexer != {}: + method, args = indexer.popitem() + else: + method, args = "", [] + params.attrs["time_indexer"] = (method, *args) + return params