From ea23dd9b3ab86c050ed6056dc9fbf6c655f6e3ac Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Mon, 29 Jul 2024 14:58:10 -0600 Subject: [PATCH] Allow importing from xarray.groupers (#9289) --- doc/api-hidden.rst | 6 +++--- doc/api.rst | 2 +- doc/conf.py | 4 ++-- xarray/__init__.py | 3 +-- xarray/core/common.py | 4 ++-- xarray/core/dataarray.py | 6 +++--- xarray/core/dataset.py | 8 ++++---- xarray/core/groupby.py | 8 ++++---- xarray/core/resample.py | 2 +- xarray/core/types.py | 2 +- xarray/{core => }/groupers.py | 0 xarray/tests/test_dataset.py | 2 +- xarray/tests/test_groupby.py | 2 +- 13 files changed, 24 insertions(+), 25 deletions(-) rename xarray/{core => }/groupers.py (100%) diff --git a/doc/api-hidden.rst b/doc/api-hidden.rst index b62fdba4bc6..c38b4f2d11c 100644 --- a/doc/api-hidden.rst +++ b/doc/api-hidden.rst @@ -694,6 +694,6 @@ coding.times.CFTimedeltaCoder coding.times.CFDatetimeCoder - core.groupers.Grouper - core.groupers.Resampler - core.groupers.EncodedGroups + groupers.Grouper + groupers.Resampler + groupers.EncodedGroups diff --git a/doc/api.rst b/doc/api.rst index c693ef84b47..6ed8d513934 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -806,7 +806,7 @@ DataArray Grouper Objects --------------- -.. currentmodule:: xarray.core +.. currentmodule:: xarray .. autosummary:: :toctree: generated/ diff --git a/doc/conf.py b/doc/conf.py index 630563f81e2..7bf8f4ceb87 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -158,8 +158,8 @@ "Variable": "~xarray.Variable", "DatasetGroupBy": "~xarray.core.groupby.DatasetGroupBy", "DataArrayGroupBy": "~xarray.core.groupby.DataArrayGroupBy", - "Grouper": "~xarray.core.groupers.Grouper", - "Resampler": "~xarray.core.groupers.Resampler", + "Grouper": "~xarray.groupers.Grouper", + "Resampler": "~xarray.groupers.Resampler", # objects without namespace: numpy "ndarray": "~numpy.ndarray", "MaskedArray": "~numpy.ma.MaskedArray", diff --git a/xarray/__init__.py b/xarray/__init__.py index 10e09bbf734..dd6e1cda1a9 100644 --- a/xarray/__init__.py +++ b/xarray/__init__.py @@ -1,6 +1,6 @@ from importlib.metadata import version as _version -from xarray import testing, tutorial +from xarray import groupers, testing, tutorial from xarray.backends.api import ( load_dataarray, load_dataset, @@ -14,7 +14,6 @@ from xarray.coding.cftimeindex import CFTimeIndex from xarray.coding.frequencies import infer_freq from xarray.conventions import SerializationWarning, decode_cf -from xarray.core import groupers from xarray.core.alignment import align, broadcast from xarray.core.combine import combine_by_coords, combine_nested from xarray.core.common import ALL_DIMS, full_like, ones_like, zeros_like diff --git a/xarray/core/common.py b/xarray/core/common.py index 52a00911d19..1e9c8ed8e29 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -38,7 +38,6 @@ from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset - from xarray.core.groupers import Resampler from xarray.core.indexes import Index from xarray.core.resample import Resample from xarray.core.rolling_exp import RollingExp @@ -53,6 +52,7 @@ T_Variable, ) from xarray.core.variable import Variable + from xarray.groupers import Resampler DTypeMaybeMapping = Union[DTypeLikeSave, Mapping[Any, DTypeLikeSave]] @@ -1048,8 +1048,8 @@ def _resample( from xarray.core.dataarray import DataArray from xarray.core.groupby import ResolvedGrouper - from xarray.core.groupers import Resampler, TimeResampler from xarray.core.resample import RESAMPLE_DIM + from xarray.groupers import Resampler, TimeResampler indexer = either_dict_or_kwargs(indexer, indexer_kwargs, "resample") if len(indexer) != 1: diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index e7498ee3465..79fd0412d40 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -88,7 +88,6 @@ from xarray.backends import ZarrStore from xarray.backends.api import T_NetcdfEngine, T_NetcdfTypes from xarray.core.groupby import DataArrayGroupBy - from xarray.core.groupers import Grouper, Resampler from xarray.core.resample import DataArrayResample from xarray.core.rolling import DataArrayCoarsen, DataArrayRolling from xarray.core.types import ( @@ -112,6 +111,7 @@ T_Xarray, ) from xarray.core.weighted import DataArrayWeighted + from xarray.groupers import Grouper, Resampler from xarray.namedarray.parallelcompat import ChunkManagerEntrypoint T_XarrayOther = TypeVar("T_XarrayOther", bound=Union["DataArray", Dataset]) @@ -6788,7 +6788,7 @@ def groupby( ResolvedGrouper, _validate_groupby_squeeze, ) - from xarray.core.groupers import UniqueGrouper + from xarray.groupers import UniqueGrouper _validate_groupby_squeeze(squeeze) @@ -6893,7 +6893,7 @@ def groupby_bins( ResolvedGrouper, _validate_groupby_squeeze, ) - from xarray.core.groupers import BinGrouper + from xarray.groupers import BinGrouper _validate_groupby_squeeze(squeeze) grouper = BinGrouper( diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ca94d0006a4..cad2f00ccc1 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -138,7 +138,6 @@ from xarray.backends.api import T_NetcdfEngine, T_NetcdfTypes from xarray.core.dataarray import DataArray from xarray.core.groupby import DatasetGroupBy - from xarray.core.groupers import Grouper, Resampler from xarray.core.merge import CoercibleMapping, CoercibleValue, _MergeResult from xarray.core.resample import DatasetResample from xarray.core.rolling import DatasetCoarsen, DatasetRolling @@ -166,6 +165,7 @@ T_Xarray, ) from xarray.core.weighted import DatasetWeighted + from xarray.groupers import Grouper, Resampler from xarray.namedarray.parallelcompat import ChunkManagerEntrypoint @@ -2709,7 +2709,7 @@ def chunk( dask.array.from_array """ from xarray.core.dataarray import DataArray - from xarray.core.groupers import TimeResampler + from xarray.groupers import TimeResampler if chunks is None and not chunks_kwargs: warnings.warn( @@ -10370,7 +10370,7 @@ def groupby( ResolvedGrouper, _validate_groupby_squeeze, ) - from xarray.core.groupers import UniqueGrouper + from xarray.groupers import UniqueGrouper _validate_groupby_squeeze(squeeze) @@ -10473,7 +10473,7 @@ def groupby_bins( ResolvedGrouper, _validate_groupby_squeeze, ) - from xarray.core.groupers import BinGrouper + from xarray.groupers import BinGrouper _validate_groupby_squeeze(squeeze) grouper = BinGrouper( diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index ca1006b9978..9b0758d030b 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -52,9 +52,9 @@ from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset - from xarray.core.groupers import Grouper from xarray.core.types import GroupIndex, GroupIndices, GroupKey from xarray.core.utils import Frozen + from xarray.groupers import Grouper def check_reduce_dims(reduce_dims, dimensions): @@ -581,7 +581,7 @@ def _iter_grouped(self) -> Iterator[T_Xarray]: yield self._obj.isel({self._group_dim: indices}) def _infer_concat_args(self, applied_example): - from xarray.core.groupers import BinGrouper + from xarray.groupers import BinGrouper (grouper,) = self.groupers if self._group_dim in applied_example.dims: @@ -695,7 +695,7 @@ def _maybe_restore_empty_groups(self, combined): """Our index contained empty groups (e.g., from a resampling or binning). If we reduced on that dimension, we want to restore the full index. """ - from xarray.core.groupers import BinGrouper, TimeResampler + from xarray.groupers import BinGrouper, TimeResampler (grouper,) = self.groupers if ( @@ -731,7 +731,7 @@ def _flox_reduce( from flox.xarray import xarray_reduce from xarray.core.dataset import Dataset - from xarray.core.groupers import BinGrouper + from xarray.groupers import BinGrouper obj = self._original_obj (grouper,) = self.groupers diff --git a/xarray/core/resample.py b/xarray/core/resample.py index a175b28412c..86b550466da 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -15,7 +15,7 @@ from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset -from xarray.core.groupers import RESAMPLE_DIM +from xarray.groupers import RESAMPLE_DIM class Resample(GroupBy[T_Xarray]): diff --git a/xarray/core/types.py b/xarray/core/types.py index 8afe034d4e3..591320d26da 100644 --- a/xarray/core/types.py +++ b/xarray/core/types.py @@ -39,10 +39,10 @@ from xarray.core.coordinates import Coordinates from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset - from xarray.core.groupers import TimeResampler from xarray.core.indexes import Index, Indexes from xarray.core.utils import Frozen from xarray.core.variable import Variable + from xarray.groupers import TimeResampler try: from dask.array import Array as DaskArray diff --git a/xarray/core/groupers.py b/xarray/groupers.py similarity index 100% rename from xarray/core/groupers.py rename to xarray/groupers.py diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 0468dccff89..f859ff38ccb 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -38,10 +38,10 @@ from xarray.core import dtypes, indexing, utils from xarray.core.common import duck_array_ops, full_like from xarray.core.coordinates import Coordinates, DatasetCoordinates -from xarray.core.groupers import TimeResampler from xarray.core.indexes import Index, PandasIndex from xarray.core.types import ArrayLike from xarray.core.utils import is_scalar +from xarray.groupers import TimeResampler from xarray.namedarray.pycompat import array_type, integer_types from xarray.testing import _assert_internal_invariants from xarray.tests import ( diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index fc0740605d6..8d147b1254e 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -12,8 +12,8 @@ import xarray as xr from xarray import DataArray, Dataset, Variable from xarray.core.groupby import _consolidate_slices -from xarray.core.groupers import BinGrouper, EncodedGroups, Grouper, UniqueGrouper from xarray.core.types import InterpOptions +from xarray.groupers import BinGrouper, EncodedGroups, Grouper, UniqueGrouper from xarray.tests import ( InaccessibleArray, assert_allclose,