Skip to content

Commit 26f91a5

Browse files
authored
Fix nanmedian for all-NaN groups (#495)
1 parent 1bfdff0 commit 26f91a5

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

flox/aggregate_flox.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ def quantile_(array, inv_idx, *, q, axis, skipna, group_idx, dtype=None, out=Non
123123
# TODO: could support all the interpolations here
124124
gamma = np.broadcast_to(virtual_index, idxshape) - lo_
125125
result = _lerp(loval, hival, t=gamma, out=out, dtype=dtype)
126+
tomask = actual_sizes == -1
126127
if not skipna and np.any(nanmask):
127-
result[..., nanmask] = np.nan
128+
tomask |= nanmask
129+
result[..., tomask] = np.nan
128130
return result
129131

130132

tests/test_xarray.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,3 +825,22 @@ def test_xarray_indexing_array_support():
825825
coords={"labels": ("y", ["a", "a", "b", "b"])},
826826
)
827827
assert is_supported_aggregation(da.variable._data, "sum")
828+
829+
830+
def test_nanmedian_gh_494():
831+
n = 100
832+
dt = pd.date_range("2020-01-01", periods=n, freq="D")
833+
834+
data = np.random.randn(n)
835+
data[0:35] = np.nan # groups 0, 1, 2 fully NaN; group 3 partially NaN
836+
data[50:80] = np.nan # groups 5, 6, 7 fully NaN <-- interior all-NaN groups
837+
data[90:] = np.nan # group 9 fully NaN
838+
839+
da = xr.DataArray(data, dims=["time"], coords={"time": dt}, name="data")
840+
group = xr.DataArray([i // 10 for i in range(n)], dims=["time"], coords={"time": dt}, name="group")
841+
842+
result_flox = xarray_reduce(da, group, func="nanmedian").compute().to_pandas()
843+
844+
result_pandas = pd.Series(data).rename("data").groupby(group.values).agg("median")
845+
result_pandas.index.name = "group"
846+
pd.testing.assert_series_equal(result_flox, result_pandas)

0 commit comments

Comments
 (0)