Skip to content

Commit bab603a

Browse files
committed
Delete resample_reduce
Closes #245
1 parent 096c080 commit bab603a

File tree

2 files changed

+6
-100
lines changed

2 files changed

+6
-100
lines changed

flox/xarray.py

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import warnings
43
from typing import TYPE_CHECKING, Any, Hashable, Iterable, Sequence, Union
54

65
import numpy as np
@@ -21,7 +20,6 @@
2120
from .xrutils import _contains_cftime_datetimes, _to_pytimedelta, datetime_to_numeric
2221

2322
if TYPE_CHECKING:
24-
from xarray.core.resample import Resample
2523
from xarray.core.types import T_DataArray, T_Dataset
2624

2725
Dims = Union[str, Iterable[Hashable], None]
@@ -450,7 +448,11 @@ def wrapper(array, *by, func, skipna, core_dims, **kwargs):
450448
actual = actual.drop_vars(name)
451449
# When grouping by MultiIndex, expect is an pd.Index wrapping
452450
# an object array of tuples
453-
if name in ds_broad.indexes and isinstance(ds_broad.indexes[name], pd.MultiIndex):
451+
if (
452+
name in ds_broad.indexes
453+
and isinstance(ds_broad.indexes[name], pd.MultiIndex)
454+
and not isinstance(expect, pd.RangeIndex)
455+
):
454456
levelnames = ds_broad.indexes[name].names
455457
expect = pd.MultiIndex.from_tuples(expect.values, names=levelnames)
456458
actual[name] = expect
@@ -582,43 +584,3 @@ def _rechunk(func, obj, dim, labels, **kwargs):
582584
)
583585

584586
return obj
585-
586-
587-
def resample_reduce(
588-
resampler: Resample,
589-
func: str | Aggregation,
590-
keep_attrs: bool = True,
591-
**kwargs,
592-
):
593-
warnings.warn(
594-
"flox.xarray.resample_reduce is now deprecated. Please use Xarray's resample method directly.",
595-
DeprecationWarning,
596-
)
597-
598-
obj = resampler._obj
599-
dim = resampler._group_dim
600-
601-
# this creates a label DataArray since resample doesn't do that somehow
602-
tostack = []
603-
for idx, slicer in enumerate(resampler._group_indices):
604-
if slicer.stop is None:
605-
stop = resampler._obj.sizes[dim]
606-
else:
607-
stop = slicer.stop
608-
tostack.append(idx * np.ones((stop - slicer.start,), dtype=np.int32))
609-
by = xr.DataArray(np.hstack(tostack), dims=(dim,), name="__resample_dim__")
610-
611-
result = (
612-
xarray_reduce(
613-
obj,
614-
by,
615-
func=func,
616-
method="blockwise",
617-
keep_attrs=keep_attrs,
618-
**kwargs,
619-
)
620-
.rename({"__resample_dim__": dim})
621-
.transpose(dim, ...)
622-
)
623-
result[dim] = resampler._unique_coord.data
624-
return result

tests/test_xarray.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xr = pytest.importorskip("xarray")
77
# isort: on
88

9-
from flox.xarray import rechunk_for_blockwise, resample_reduce, xarray_reduce
9+
from flox.xarray import rechunk_for_blockwise, xarray_reduce
1010

1111
from . import assert_equal, has_dask, raise_if_dask_computes, requires_dask
1212

@@ -245,48 +245,6 @@ def test_xarray_reduce_errors():
245245
xarray_reduce(da, by.chunk(), func="mean")
246246

247247

248-
@pytest.mark.parametrize("isdask", [True, False])
249-
@pytest.mark.parametrize("dataarray", [True, False])
250-
@pytest.mark.parametrize("chunklen", [27, 4 * 31 + 1, 4 * 31 + 20])
251-
def test_xarray_resample(chunklen, isdask, dataarray, engine):
252-
if isdask:
253-
if not has_dask:
254-
pytest.skip()
255-
ds = xr.tutorial.open_dataset("air_temperature", chunks={"time": chunklen})
256-
else:
257-
ds = xr.tutorial.open_dataset("air_temperature")
258-
259-
if dataarray:
260-
ds = ds.air
261-
262-
resampler = ds.resample(time="M")
263-
with pytest.warns(DeprecationWarning):
264-
actual = resample_reduce(resampler, "mean", engine=engine)
265-
expected = resampler.mean()
266-
xr.testing.assert_allclose(actual, expected)
267-
268-
with xr.set_options(use_flox=True):
269-
actual = resampler.mean()
270-
xr.testing.assert_allclose(actual, expected)
271-
272-
273-
@requires_dask
274-
def test_xarray_resample_dataset_multiple_arrays(engine):
275-
# regression test for #35
276-
times = pd.date_range("2000", periods=5)
277-
foo = xr.DataArray(range(5), dims=["time"], coords=[times], name="foo")
278-
bar = xr.DataArray(range(1, 6), dims=["time"], coords=[times], name="bar")
279-
ds = xr.merge([foo, bar]).chunk({"time": 4})
280-
281-
resampler = ds.resample(time="4D")
282-
# The separate computes are necessary here to force xarray
283-
# to compute all variables in result at the same time.
284-
expected = resampler.mean().compute()
285-
with pytest.warns(DeprecationWarning):
286-
result = resample_reduce(resampler, "mean", engine=engine).compute()
287-
xr.testing.assert_allclose(expected, result)
288-
289-
290248
@requires_dask
291249
@pytest.mark.parametrize(
292250
"inchunks, expected",
@@ -439,20 +397,6 @@ def test_cache():
439397
assert len(cache.data) == 2
440398

441399

442-
@pytest.mark.parametrize("use_cftime", [True, False])
443-
@pytest.mark.parametrize("func", ["count", "mean"])
444-
def test_datetime_array_reduce(use_cftime, func, engine):
445-
time = xr.DataArray(
446-
xr.date_range("2009-01-01", "2012-12-31", use_cftime=use_cftime),
447-
dims=("time",),
448-
name="time",
449-
)
450-
expected = getattr(time.resample(time="YS"), func)()
451-
with pytest.warns(DeprecationWarning):
452-
actual = resample_reduce(time.resample(time="YS"), func=func, engine=engine)
453-
assert_equal(expected, actual)
454-
455-
456400
@requires_dask
457401
@pytest.mark.parametrize("method", ["cohorts", "map-reduce"])
458402
def test_groupby_bins_indexed_coordinate(method):

0 commit comments

Comments
 (0)