Skip to content

Commit aeb00f9

Browse files
pierreloicqIllviljanspencerkclarkpre-commit-ci[bot]
authored
_season_from_months can now handle np.nan (pydata#5876)
* _season_from_months can now handle np.nan _season_from_months can now handle np.nan and values outside of [1,12] I passed these tests: def test_season(): months = np.array([ 1, 2, 3, 4, 5, np.nan]) assert ( _season_from_months(months) == np.array(['DJF', 'DJF', 'MAM', 'MAM', 'MAM', 'na']) ).all() months = np.array([ 1, 100, 3, 13, 0, -5]) assert ( _season_from_months(months) == np.array(['DJF', 'na', 'MAM', 'na', 'na', 'na']) ).all() months = np.array(range(1, 13)) assert ( _season_from_months(months) == np.array(['DJF', 'DJF', 'MAM', 'MAM', 'MAM', 'JJA', 'JJA', 'JJA', 'SON', 'SON', 'SON', 'DJF']) ).all() test_season() * Run black * Remove duplicated import * returns np.nan and removed the useless attribution returns np.nan and removed the useless attribution when month is not in [1,12] * added test * applied black recommendations * Apply suggestions from code review finally, NaT is output as a "nan" string Co-authored-by: Spencer Clark <spencerkclark@gmail.com> * update whatsnew * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Correction on the credits Co-authored-by: Spencer Clark <spencerkclark@gmail.com> Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> Co-authored-by: Spencer Clark <spencerkclark@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5c08ab2 commit aeb00f9

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Bug fixes
5353
- Fix applying function with non-xarray arguments using :py:func:`xr.map_blocks`.
5454
By `Cindy Chiao <https://github.com/tcchiao>`_.
5555

56+
- `dt.season <https://xarray.pydata.org/en/stable/generated/xarray.DataArray.dt.season.html>`_ can now handle NaN and NaT. (:pull:`5876`).
57+
By `Pierre Loicq <https://github.com/pierreloicq>`_.
58+
59+
5660
Documentation
5761
~~~~~~~~~~~~~
5862

xarray/core/accessor_dt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,20 @@
1616
def _season_from_months(months):
1717
"""Compute season (DJF, MAM, JJA, SON) from month ordinal"""
1818
# TODO: Move "season" accessor upstream into pandas
19-
seasons = np.array(["DJF", "MAM", "JJA", "SON"])
19+
seasons = np.array(["DJF", "MAM", "JJA", "SON", "nan"])
2020
months = np.asarray(months)
21-
return seasons[(months // 3) % 4]
21+
22+
with warnings.catch_warnings():
23+
warnings.filterwarnings(
24+
"ignore", message="invalid value encountered in floor_divide"
25+
)
26+
warnings.filterwarnings(
27+
"ignore", message="invalid value encountered in remainder"
28+
)
29+
idx = (months // 3) % 4
30+
31+
idx[np.isnan(idx)] = 4
32+
return seasons[idx.astype(int)]
2233

2334

2435
def _access_through_cftimeindex(values, name):

xarray/tests/test_accessor_dt.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def test_dask_accessor_method(self, method, parameters) -> None:
222222

223223
def test_seasons(self) -> None:
224224
dates = pd.date_range(start="2000/01/01", freq="M", periods=12)
225+
dates = dates.append(pd.Index([np.datetime64("NaT")]))
225226
dates = xr.DataArray(dates)
226227
seasons = xr.DataArray(
227228
[
@@ -237,6 +238,7 @@ def test_seasons(self) -> None:
237238
"SON",
238239
"SON",
239240
"DJF",
241+
"nan",
240242
]
241243
)
242244

0 commit comments

Comments
 (0)