Skip to content

_season_from_months can now handle np.nan #5876

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 12 commits into from
Jan 11, 2022
14 changes: 12 additions & 2 deletions xarray/core/accessor_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import pandas as pd
import warnings

from .common import (
_contains_datetime_like_objects,
Expand All @@ -16,9 +17,18 @@
def _season_from_months(months):
"""Compute season (DJF, MAM, JJA, SON) from month ordinal"""
# TODO: Move "season" accessor upstream into pandas
seasons = np.array(["DJF", "MAM", "JJA", "SON"])
seasons = np.array(["DJF", "MAM", "JJA", "SON", "na"])
months = np.asarray(months)
return seasons[(months // 3) % 4]

idx = 0
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message='invalid value encountered in floor_divide')
warnings.filterwarnings('ignore', message='invalid value encountered in remainder')
idx = (months // 3) % 4

idx[ np.isnan(idx) ] = 4
idx[ (months >= 13) | (months <= 0) ] = 4
return seasons[ idx.astype(int) ]


def _access_through_cftimeindex(values, name):
Expand Down