Repository navigation
Conversation
np.sum on a pandas Series skips NaN, so nse and r2 returned 1.0 when model values were missing, while NumPy input returned NaN. Convert the inputs with np.asarray first. Fixes DHI#720
|
Thanks for the PR. Please describe in what real modelling context this is a problem, and how this PR solves it. |
|
Thanks for taking a look. To be precise about where this shows up: Where it doesn't: Where it does:
What the PR changes: If you'd rather handle it at the source, an alternative is to have |
|
Thanks for taking the time to look into this. We're handling this issue internally, so I'm closing this PR. |
Fixes #720
nash_sutcliffe_efficiencyandr2callednp.sumdirectly on their inputs. On a pandas Series that skips NaN, so a model with missing values (or an all-NaN model) scored 1.0. NumPy input gave NaN. Both functions now convertobsandmodelwithnp.asarraybefore computing, as the issue suggests, so Series and arrays give the same result.Tests: I added
test_nse_r2_nan_in_model_series, parametrized overnse/r2and over partly-NaN and all-NaN model values. It checks that Series input returns NaN, the same as ndarray input. It fails onmainand passes with this change. The existing metric tests, themetrics.pydoctests,ruff check/ruff format --checkand mypy onmetrics.pyall pass locally.Scope note: I kept this to the two metrics in the issue. When I compared every metric on Series against ndarray with the same input, a few others also differ when model values are NaN:
bias,max_error,mape,explained_variance,kge,scatter_index/scatter_index2andwillmott. Happy to follow up with the same conversion for those if you'd like them handled consistently, in this PR or a separate one.