diff --git a/doc/user-guide/combining.rst b/doc/user-guide/combining.rst index cc4fd3adcf4..993527ede4e 100644 --- a/doc/user-guide/combining.rst +++ b/doc/user-guide/combining.rst @@ -64,7 +64,10 @@ dimension: .. jupyter-execute:: - xr.concat([da.isel(x=0), da.isel(x=1)], "new_dim") + da0 = da.isel(x=0, drop=True) + da1 = da.isel(x=1, drop=True) + + xr.concat([da0, da1], "new_dim") The second argument to ``concat`` can also be an :py:class:`~pandas.Index` or :py:class:`~xarray.DataArray` object as well as a string, in which case it is @@ -72,7 +75,7 @@ used to label the values along the new dimension: .. jupyter-execute:: - xr.concat([da.isel(x=0), da.isel(x=1)], pd.Index([-90, -100], name="new_dim")) + xr.concat([da0, da1], pd.Index([-90, -100], name="new_dim")) Of course, ``concat`` also works on ``Dataset`` objects: @@ -87,6 +90,12 @@ between datasets. With the default parameters, xarray will load some coordinate variables into memory to compare them between datasets. This may be prohibitively expensive if you are manipulating your dataset lazily using :ref:`dask`. +.. note:: + + In a future version of xarray the default values for many of these options + will change. You can opt into the new default values early using + ``xr.set_options(use_new_combine_kwarg_defaults=True)``. + .. _merge: Merge @@ -109,10 +118,18 @@ If you merge another dataset (or a dictionary including data array objects), by default the resulting dataset will be aligned on the **union** of all index coordinates: +.. note:: + + In a future version of xarray the default value for ``join`` and ``compat`` + will change. This change will mean that xarray will no longer attempt + to align the indices of the merged dataset. You can opt into the new default + values early using ``xr.set_options(use_new_combine_kwarg_defaults=True)``. + Or explicitly set ``join='outer'`` to preserve old behavior. + .. jupyter-execute:: other = xr.Dataset({"bar": ("x", [1, 2, 3, 4]), "x": list("abcd")}) - xr.merge([ds, other]) + xr.merge([ds, other], join="outer") This ensures that ``merge`` is non-destructive. ``xarray.MergeError`` is raised if you attempt to merge two variables with the same name but different values: @@ -123,6 +140,16 @@ if you attempt to merge two variables with the same name but different values: xr.merge([ds, ds + 1]) +.. note:: + + In a future version of xarray the default value for ``compat`` will change + from ``compat='no_conflicts'`` to ``compat='override'``. In this scenario + the values in the first object override all the values in other objects. + + .. jupyter-execute:: + + xr.merge([ds, ds + 1], compat="override") + The same non-destructive merging between ``DataArray`` index coordinates is used in the :py:class:`~xarray.Dataset` constructor: @@ -156,6 +183,11 @@ For datasets, ``ds0.combine_first(ds1)`` works similarly to there are conflicting values in variables to be merged, whereas ``.combine_first`` defaults to the calling object's values. +.. note:: + + In a future version of xarray the default options for ``xr.merge`` will change + such that the behavior matches ``combine_first``. + .. _update: Update @@ -248,7 +280,7 @@ coordinates as long as any non-missing values agree or are disjoint: ds1 = xr.Dataset({"a": ("x", [10, 20, 30, np.nan])}, {"x": [1, 2, 3, 4]}) ds2 = xr.Dataset({"a": ("x", [np.nan, 30, 40, 50])}, {"x": [2, 3, 4, 5]}) - xr.merge([ds1, ds2], compat="no_conflicts") + xr.merge([ds1, ds2], join="outer", compat="no_conflicts") Note that due to the underlying representation of missing values as floating point numbers (``NaN``), variable data type is not always preserved when merging @@ -311,12 +343,11 @@ coordinates, not on their position in the list passed to ``combine_by_coords``. .. jupyter-execute:: - x1 = xr.DataArray(name="foo", data=np.random.randn(3), coords=[("x", [0, 1, 2])]) x2 = xr.DataArray(name="foo", data=np.random.randn(3), coords=[("x", [3, 4, 5])]) xr.combine_by_coords([x2, x1]) -These functions can be used by :py:func:`~xarray.open_mfdataset` to open many +These functions are used by :py:func:`~xarray.open_mfdataset` to open many files as one dataset. The particular function used is specified by setting the argument ``'combine'`` to ``'by_coords'`` or ``'nested'``. This is useful for situations where your data is split across many files in multiple locations, diff --git a/doc/user-guide/terminology.rst b/doc/user-guide/terminology.rst index 1c1b930c9c7..02473d5d28e 100644 --- a/doc/user-guide/terminology.rst +++ b/doc/user-guide/terminology.rst @@ -220,7 +220,7 @@ complete examples, please consult the relevant documentation.* ) # combine the datasets - combined_ds = xr.combine_by_coords([ds1, ds2]) + combined_ds = xr.combine_by_coords([ds1, ds2], join="outer") combined_ds lazy diff --git a/doc/whats-new.rst b/doc/whats-new.rst index f5469fa5227..06a3c2cb22d 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -21,6 +21,18 @@ Breaking changes Deprecations ~~~~~~~~~~~~ +- Start a deprecation cycle for changing the default keyword arguments to :py:func:`concat`, :py:func:`merge`, + :py:func:`combine_nested`, :py:func:`combine_by_coords`, and :py:func:`open_mfdataset`. + Emits a :py:class:`FutureWarning` when using old defaults and new defaults would result in different behavior. + Adds an option: ``use_new_combine_kwarg_defaults`` to opt in to new defaults immediately. + New values are: + + - ``data_vars``: None which means ``all`` when concatenating along a new dimension, and ``"minimal"`` when concatenating along an existing dimension + - ``coords``: "minimal" + - ``compat``: "override" + - ``join``: "exact" + + (:issue:`8778`, :issue:`1385`, :pull:`10062`). By `Julia Signell `_. Bug fixes ~~~~~~~~~ @@ -8425,8 +8437,15 @@ Backwards incompatible changes .. code:: python - ds = xray.Dataset({"x": 0}) + In [1]: ds = xray.Dataset({"x": 0}) + In [2]: xray.concat([ds, ds], dim="y") + Out[2]: + Size: 16B + Dimensions: (y: 2) + Dimensions without coordinates: y + Data variables: + x (y) int64 16B 0 0 .. code:: python xray.concat([ds, ds], dim="y") diff --git a/xarray/backends/api.py b/xarray/backends/api.py index 9aa37115a37..328b7568cdd 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -36,7 +36,7 @@ ) from xarray.backends.locks import _get_scheduler from xarray.coders import CFDatetimeCoder, CFTimedeltaCoder -from xarray.core import indexing +from xarray.core import dtypes, indexing from xarray.core.coordinates import Coordinates from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset @@ -53,6 +53,13 @@ _nested_combine, combine_by_coords, ) +from xarray.util.deprecation_helpers import ( + _COMPAT_DEFAULT, + _COORDS_DEFAULT, + _DATA_VARS_DEFAULT, + _JOIN_DEFAULT, + CombineKwargDefault, +) if TYPE_CHECKING: try: @@ -1459,14 +1466,17 @@ def open_mfdataset( | Sequence[Index] | None ) = None, - compat: CompatOptions = "no_conflicts", + compat: CompatOptions | CombineKwargDefault = _COMPAT_DEFAULT, preprocess: Callable[[Dataset], Dataset] | None = None, engine: T_Engine = None, - data_vars: Literal["all", "minimal", "different"] | list[str] = "all", - coords="different", + data_vars: Literal["all", "minimal", "different"] + | None + | list[str] + | CombineKwargDefault = _DATA_VARS_DEFAULT, + coords=_COORDS_DEFAULT, combine: Literal["by_coords", "nested"] = "by_coords", parallel: bool = False, - join: JoinOptions = "outer", + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, attrs_file: str | os.PathLike | None = None, combine_attrs: CombineAttrsOptions = "override", **kwargs, @@ -1711,6 +1721,7 @@ def open_mfdataset( ids=ids, join=join, combine_attrs=combine_attrs, + fill_value=dtypes.NA, ) elif combine == "by_coords": # Redo ordering from coordinates, ignoring how they were ordered diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 5355d81c6c0..acc7d1f17f6 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -122,7 +122,13 @@ merge_coordinates_without_align, merge_data_and_coords, ) -from xarray.util.deprecation_helpers import _deprecate_positional_args, deprecate_dims +from xarray.util.deprecation_helpers import ( + _COMPAT_DEFAULT, + _JOIN_DEFAULT, + CombineKwargDefault, + _deprecate_positional_args, + deprecate_dims, +) if TYPE_CHECKING: from dask.dataframe import DataFrame as DaskDataFrame @@ -5321,7 +5327,14 @@ def stack_dataarray(da): # concatenate the arrays stackable_vars = [stack_dataarray(da) for da in self.data_vars.values()] - data_array = concat(stackable_vars, dim=new_dim) + data_array = concat( + stackable_vars, + dim=new_dim, + data_vars="all", + coords="different", + compat="equals", + join="outer", + ) if name is not None: data_array.name = name @@ -5565,8 +5578,8 @@ def merge( self, other: CoercibleMapping | DataArray, overwrite_vars: Hashable | Iterable[Hashable] = frozenset(), - compat: CompatOptions = "no_conflicts", - join: JoinOptions = "outer", + compat: CompatOptions | CombineKwargDefault = _COMPAT_DEFAULT, + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, fill_value: Any = xrdtypes.NA, combine_attrs: CombineAttrsOptions = "override", ) -> Self: diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 446fd1343cc..6ff5d499179 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -1628,7 +1628,14 @@ def _combine(self, applied, shortcut=False): if shortcut: combined = self._concat_shortcut(applied, dim, positions) else: - combined = concat(applied, dim) + combined = concat( + applied, + dim, + data_vars="all", + coords="different", + compat="equals", + join="outer", + ) combined = _maybe_reorder(combined, dim, positions, N=self.group1d.size) if isinstance(combined, type(self._obj)): @@ -1789,7 +1796,14 @@ def _combine(self, applied): """Recombine the applied objects like the original.""" applied_example, applied = peek_at(applied) dim, positions = self._infer_concat_args(applied_example) - combined = concat(applied, dim) + combined = concat( + applied, + dim, + data_vars="all", + coords="different", + compat="equals", + join="outer", + ) combined = _maybe_reorder(combined, dim, positions, N=self.group1d.size) # assign coord when the applied function does not return that coord if dim not in applied_example.dims: diff --git a/xarray/core/options.py b/xarray/core/options.py index 90775360ab5..791d3bd928e 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -30,6 +30,7 @@ "keep_attrs", "warn_for_unclosed_files", "use_bottleneck", + "use_new_combine_kwarg_defaults", "use_numbagg", "use_opt_einsum", "use_flox", @@ -59,6 +60,7 @@ class T_Options(TypedDict): warn_for_unclosed_files: bool use_bottleneck: bool use_flox: bool + use_new_combine_kwarg_defaults: bool use_numbagg: bool use_opt_einsum: bool @@ -87,6 +89,7 @@ class T_Options(TypedDict): "warn_for_unclosed_files": False, "use_bottleneck": True, "use_flox": True, + "use_new_combine_kwarg_defaults": False, "use_numbagg": True, "use_opt_einsum": True, } @@ -117,6 +120,7 @@ def _positive_integer(value: Any) -> bool: "file_cache_maxsize": _positive_integer, "keep_attrs": lambda choice: choice in [True, False, "default"], "use_bottleneck": lambda value: isinstance(value, bool), + "use_new_combine_kwarg_defaults": lambda value: isinstance(value, bool), "use_numbagg": lambda value: isinstance(value, bool), "use_opt_einsum": lambda value: isinstance(value, bool), "use_flox": lambda value: isinstance(value, bool), @@ -256,6 +260,15 @@ class set_options: use_flox : bool, default: True Whether to use ``numpy_groupies`` and `flox`` to accelerate groupby and resampling reductions. + use_new_combine_kwarg_defaults : bool, default False + Whether to use new kwarg default values for combine functions: + :py:func:`~xarray.concat`, :py:func:`~xarray.merge`, + :py:func:`~xarray.open_mfdataset`. New values are: + + * ``data_vars``: None + * ``coords``: "minimal" + * ``compat``: "override" + * ``join``: "exact" use_numbagg : bool, default: True Whether to use ``numbagg`` to accelerate reductions. Takes precedence over ``use_bottleneck`` when both are True. diff --git a/xarray/core/parallel.py b/xarray/core/parallel.py index 8bb98118081..a718ef5e911 100644 --- a/xarray/core/parallel.py +++ b/xarray/core/parallel.py @@ -351,7 +351,9 @@ def _wrapper( result = func(*converted_args, **kwargs) merged_coordinates = merge( - [arg.coords for arg in args if isinstance(arg, Dataset | DataArray)] + [arg.coords for arg in args if isinstance(arg, Dataset | DataArray)], + join="exact", + compat="override", ).coords # check all dims are present @@ -441,7 +443,11 @@ def _wrapper( # rechunk any numpy variables appropriately xarray_objs = tuple(arg.chunk(arg.chunksizes) for arg in xarray_objs) - merged_coordinates = merge([arg.coords for arg in aligned]).coords + merged_coordinates = merge( + [arg.coords for arg in aligned], + join="exact", + compat="override", + ).coords _, npargs = unzip( sorted( @@ -474,7 +480,10 @@ def _wrapper( ) coordinates = merge( - (preserved_coords, template.coords.to_dataset()[new_coord_vars]) + (preserved_coords, template.coords.to_dataset()[new_coord_vars]), + # FIXME: this should be join="exact", but breaks a test + join="outer", + compat="override", ).coords output_chunks: Mapping[Hashable, tuple[int, ...]] = { dim: input_chunks[dim] for dim in template.dims if dim in input_chunks diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index 537fbd5bafb..301cb5b501f 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -196,7 +196,13 @@ def _prepare_plot1d_data( dim = coords_to_plot.get(v, None) if (dim is not None) and (dim in darray.dims): darray_nan = np.nan * darray.isel({dim: -1}) - darray = concat([darray, darray_nan], dim=dim) + darray = concat( + [darray, darray_nan], + dim=dim, + coords="minimal", + compat="override", + join="exact", + ) dims_T.append(coords_to_plot[v]) # Lines should never connect to the same coordinate when stacked, diff --git a/xarray/structure/alignment.py b/xarray/structure/alignment.py index b05271dfdf5..5cda7c25681 100644 --- a/xarray/structure/alignment.py +++ b/xarray/structure/alignment.py @@ -6,7 +6,7 @@ from collections.abc import Callable, Hashable, Iterable, Mapping from contextlib import suppress from itertools import starmap -from typing import TYPE_CHECKING, Any, Final, Generic, TypeVar, cast, overload +from typing import TYPE_CHECKING, Any, Final, Generic, TypeVar, cast, get_args, overload import numpy as np import pandas as pd @@ -20,9 +20,10 @@ indexes_all_equal, safe_cast_to_index, ) -from xarray.core.types import T_Alignable -from xarray.core.utils import is_dict_like, is_full_slice +from xarray.core.types import JoinOptions, T_Alignable +from xarray.core.utils import emit_user_level_warning, is_dict_like, is_full_slice from xarray.core.variable import Variable, as_compatible_data, calculate_dimensions +from xarray.util.deprecation_helpers import CombineKwargDefault if TYPE_CHECKING: from xarray.core.dataarray import DataArray @@ -155,11 +156,10 @@ class Aligner(Generic[T_Alignable]): objects: tuple[T_Alignable, ...] results: tuple[T_Alignable, ...] - objects_matching_indexes: tuple[dict[MatchingIndexKey, Index], ...] objects_matching_index_vars: tuple[ dict[MatchingIndexKey, dict[Hashable, Variable]], ... ] - join: str + join: JoinOptions | CombineKwargDefault exclude_dims: frozenset[Hashable] exclude_vars: frozenset[Hashable] copy: bool @@ -180,7 +180,7 @@ class Aligner(Generic[T_Alignable]): def __init__( self, objects: Iterable[T_Alignable], - join: str = "inner", + join: JoinOptions | CombineKwargDefault = "inner", indexes: Mapping[Any, Any] | None = None, exclude_dims: str | Iterable[Hashable] = frozenset(), exclude_vars: Iterable[Hashable] = frozenset(), @@ -191,10 +191,12 @@ def __init__( sparse: bool = False, ): self.objects = tuple(objects) - self.objects_matching_indexes = () + self.objects_matching_indexes: tuple[Any, ...] = () self.objects_matching_index_vars = () - if join not in ["inner", "outer", "override", "exact", "left", "right"]: + if not isinstance(join, CombineKwargDefault) and join not in get_args( + JoinOptions + ): raise ValueError(f"invalid value for join: {join}") self.join = join @@ -451,12 +453,34 @@ def update_dicts( else: need_reindex = False if need_reindex: + if ( + isinstance(self.join, CombineKwargDefault) + and self.join != "exact" + ): + emit_user_level_warning( + self.join.warning_message( + "This change will result in the following ValueError: " + "cannot be aligned with join='exact' because " + "index/labels/sizes are not equal along " + "these coordinates (dimensions): " + + ", ".join( + f"{name!r} {dims!r}" for name, dims in key[0] + ), + recommend_set_options=False, + ), + FutureWarning, + ) if self.join == "exact": raise AlignmentError( "cannot align objects with join='exact' where " "index/labels/sizes are not equal along " "these coordinates (dimensions): " + ", ".join(f"{name!r} {dims!r}" for name, dims in key[0]) + + ( + self.join.error_message() + if isinstance(self.join, CombineKwargDefault) + else "" + ) ) joiner = self._get_index_joiner(index_cls) joined_index = joiner(matching_indexes) @@ -656,7 +680,7 @@ def align( obj1: T_Obj1, /, *, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -670,7 +694,7 @@ def align( obj2: T_Obj2, /, *, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -685,7 +709,7 @@ def align( obj3: T_Obj3, /, *, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -701,7 +725,7 @@ def align( obj4: T_Obj4, /, *, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -718,7 +742,7 @@ def align( obj5: T_Obj5, /, *, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -729,7 +753,7 @@ def align( @overload def align( *objects: T_Alignable, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -739,7 +763,7 @@ def align( def align( *objects: T_Alignable, - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), @@ -947,7 +971,7 @@ def align( def deep_align( objects: Iterable[Any], - join: JoinOptions = "inner", + join: JoinOptions | CombineKwargDefault = "inner", copy: bool = True, indexes=None, exclude: str | Iterable[Hashable] = frozenset(), diff --git a/xarray/structure/combine.py b/xarray/structure/combine.py index 63c3e4cc166..b1dbc8e4c0e 100644 --- a/xarray/structure/combine.py +++ b/xarray/structure/combine.py @@ -10,8 +10,16 @@ from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset from xarray.core.utils import iterate_nested +from xarray.structure.alignment import AlignmentError from xarray.structure.concat import concat from xarray.structure.merge import merge +from xarray.util.deprecation_helpers import ( + _COMPAT_DEFAULT, + _COORDS_DEFAULT, + _DATA_VARS_DEFAULT, + _JOIN_DEFAULT, + CombineKwargDefault, +) if TYPE_CHECKING: from xarray.core.types import ( @@ -200,12 +208,12 @@ def _check_shape_tile_ids(combined_tile_ids): def _combine_nd( combined_ids, concat_dims, - data_vars="all", - coords="different", - compat: CompatOptions = "no_conflicts", - fill_value=dtypes.NA, - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "drop", + data_vars, + coords, + compat: CompatOptions | CombineKwargDefault, + fill_value, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, ): """ Combines an N-dimensional structure of datasets into one by applying a @@ -262,10 +270,10 @@ def _combine_all_along_first_dim( dim, data_vars, coords, - compat: CompatOptions, - fill_value=dtypes.NA, - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "drop", + compat: CompatOptions | CombineKwargDefault, + fill_value, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, ): # Group into lines of datasets which must be combined along dim grouped = groupby_defaultdict(list(combined_ids.items()), key=_new_tile_id) @@ -276,7 +284,14 @@ def _combine_all_along_first_dim( combined_ids = dict(sorted(group)) datasets = combined_ids.values() new_combined_ids[new_id] = _combine_1d( - datasets, dim, compat, data_vars, coords, fill_value, join, combine_attrs + datasets, + concat_dim=dim, + compat=compat, + data_vars=data_vars, + coords=coords, + fill_value=fill_value, + join=join, + combine_attrs=combine_attrs, ) return new_combined_ids @@ -284,12 +299,12 @@ def _combine_all_along_first_dim( def _combine_1d( datasets, concat_dim, - compat: CompatOptions = "no_conflicts", - data_vars="all", - coords="different", - fill_value=dtypes.NA, - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "drop", + compat: CompatOptions | CombineKwargDefault, + data_vars, + coords, + fill_value, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, ): """ Applies either concat or merge to 1D list of datasets depending on value @@ -320,13 +335,20 @@ def _combine_1d( else: raise else: - combined = merge( - datasets, - compat=compat, - fill_value=fill_value, - join=join, - combine_attrs=combine_attrs, - ) + try: + combined = merge( + datasets, + compat=compat, + fill_value=fill_value, + join=join, + combine_attrs=combine_attrs, + ) + except AlignmentError as e: + e.add_note( + "If you are intending to concatenate datasets, please specify the concatenation dimension explicitly. " + "Using merge to concatenate is quite inefficient." + ) + raise e return combined @@ -343,9 +365,9 @@ def _nested_combine( data_vars, coords, ids, - fill_value=dtypes.NA, - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "drop", + fill_value, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, ): if len(datasets) == 0: return Dataset() @@ -366,7 +388,7 @@ def _nested_combine( # Apply series of concatenate or merge operations along each dimension combined = _combine_nd( combined_ids, - concat_dims, + concat_dims=concat_dims, compat=compat, data_vars=data_vars, coords=coords, @@ -383,12 +405,12 @@ def _nested_combine( def combine_nested( datasets: DATASET_HYPERCUBE, - concat_dim: str | DataArray | Sequence[str | DataArray | pd.Index | None] | None, - compat: str = "no_conflicts", - data_vars: str = "all", - coords: str = "different", + concat_dim: str | DataArray | Sequence[str | DataArray | pd.Index | None], + compat: str | CombineKwargDefault = _COMPAT_DEFAULT, + data_vars: str | CombineKwargDefault = _DATA_VARS_DEFAULT, + coords: str | CombineKwargDefault = _COORDS_DEFAULT, fill_value: object = dtypes.NA, - join: JoinOptions = "outer", + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, combine_attrs: CombineAttrsOptions = "drop", ) -> Dataset: """ @@ -439,9 +461,36 @@ def combine_nested( of all non-null values. - "override": skip comparing and pick variable from first dataset data_vars : {"minimal", "different", "all" or list of str}, optional - Details are in the documentation of concat + These data variables will be concatenated together: + * "minimal": Only data variables in which the dimension already + appears are included. + * "different": Data variables which are not equal (ignoring + attributes) across all datasets are also concatenated (as well as + all for which dimension already appears). Beware: this option may + load the data payload of data variables into memory if they are not + already loaded. + * "all": All data variables will be concatenated. + * None: Means ``"all"`` if ``dim`` is not present in any of the ``objs``, + and ``"minimal"`` if ``dim`` is present in any of ``objs``. + * list of dims: The listed data variables will be concatenated, in + addition to the "minimal" data variables. + coords : {"minimal", "different", "all" or list of str}, optional - Details are in the documentation of concat + These coordinate variables will be concatenated together: + * "minimal": Only coordinates in which the dimension already appears + are included. If concatenating over a dimension _not_ + present in any of the objects, then all data variables will + be concatenated along that new dimension. + * "different": Coordinates which are not equal (ignoring attributes) + across all datasets are also concatenated (as well as all for which + dimension already appears). Beware: this option may load the data + payload of coordinate variables into memory if they are not already + loaded. + * "all": All coordinate variables will be concatenated, except + those corresponding to other dimensions. + * list of Hashable: The listed coordinate variables will be concatenated, + in addition to the "minimal" coordinates. + fill_value : scalar or dict-like, optional Value to use for newly missing values. If a dict-like, maps variable names to fill values. Use a data array's name to @@ -619,12 +668,12 @@ def groupby_defaultdict( def _combine_single_variable_hypercube( datasets, - fill_value=dtypes.NA, - data_vars="all", - coords="different", - compat: CompatOptions = "no_conflicts", - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "no_conflicts", + fill_value, + data_vars, + coords, + compat: CompatOptions | CombineKwargDefault, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, ): """ Attempt to combine a list of Datasets into a hypercube using their @@ -678,11 +727,14 @@ def _combine_single_variable_hypercube( def combine_by_coords( data_objects: Iterable[Dataset | DataArray] = [], - compat: CompatOptions = "no_conflicts", - data_vars: Literal["all", "minimal", "different"] | list[str] = "all", - coords: str = "different", + compat: CompatOptions | CombineKwargDefault = _COMPAT_DEFAULT, + data_vars: Literal["all", "minimal", "different"] + | None + | list[str] + | CombineKwargDefault = _DATA_VARS_DEFAULT, + coords: str | CombineKwargDefault = _COORDS_DEFAULT, fill_value: object = dtypes.NA, - join: JoinOptions = "outer", + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, combine_attrs: CombineAttrsOptions = "no_conflicts", ) -> Dataset | DataArray: """ @@ -862,7 +914,7 @@ def combine_by_coords( temperature (y, x) float64 96B 10.98 14.3 12.06 ... 1.743 0.4044 16.65 precipitation (y, x) float64 96B 0.4376 0.8918 0.9637 ... 0.4615 0.7805 - >>> xr.combine_by_coords([x3, x1]) + >>> xr.combine_by_coords([x3, x1], join="outer") Size: 464B Dimensions: (y: 4, x: 6) Coordinates: @@ -882,7 +934,7 @@ def combine_by_coords( temperature (y, x) float64 96B 10.98 14.3 12.06 ... 18.89 10.44 8.293 precipitation (y, x) float64 96B 0.4376 0.8918 0.9637 ... 0.01879 0.6176 - >>> xr.combine_by_coords([x1, x2, x3]) + >>> xr.combine_by_coords([x1, x2, x3], join="outer") Size: 464B Dimensions: (y: 4, x: 6) Coordinates: diff --git a/xarray/structure/concat.py b/xarray/structure/concat.py index 2f1f3c28b02..d809dd7937e 100644 --- a/xarray/structure/concat.py +++ b/xarray/structure/concat.py @@ -1,7 +1,7 @@ from __future__ import annotations from collections.abc import Hashable, Iterable -from typing import TYPE_CHECKING, Any, Union, overload +from typing import TYPE_CHECKING, Any, Literal, Union, overload import numpy as np import pandas as pd @@ -11,6 +11,7 @@ from xarray.core.duck_array_ops import lazy_array_equiv from xarray.core.indexes import Index, PandasIndex from xarray.core.types import T_DataArray, T_Dataset, T_Variable +from xarray.core.utils import emit_user_level_warning from xarray.core.variable import Variable from xarray.core.variable import concat as concat_vars from xarray.structure.alignment import align, reindex_variables @@ -20,6 +21,13 @@ merge_attrs, merge_collected, ) +from xarray.util.deprecation_helpers import ( + _COMPAT_CONCAT_DEFAULT, + _COORDS_DEFAULT, + _DATA_VARS_DEFAULT, + _JOIN_DEFAULT, + CombineKwargDefault, +) if TYPE_CHECKING: from xarray.core.types import ( @@ -29,7 +37,7 @@ JoinOptions, ) - T_DataVars = Union[ConcatOptions, Iterable[Hashable]] + T_DataVars = Union[ConcatOptions, Iterable[Hashable], None] # TODO: replace dim: Any by 1D array_likes @@ -37,12 +45,12 @@ def concat( objs: Iterable[T_Dataset], dim: Hashable | T_Variable | T_DataArray | pd.Index | Any, - data_vars: T_DataVars = "all", - coords: ConcatOptions | list[Hashable] = "different", - compat: CompatOptions = "equals", + data_vars: T_DataVars | CombineKwargDefault = _DATA_VARS_DEFAULT, + coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault = _COORDS_DEFAULT, + compat: CompatOptions | CombineKwargDefault = _COMPAT_CONCAT_DEFAULT, positions: Iterable[Iterable[int]] | None = None, fill_value: object = dtypes.NA, - join: JoinOptions = "outer", + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, combine_attrs: CombineAttrsOptions = "override", create_index_for_new_dim: bool = True, ) -> T_Dataset: ... @@ -52,12 +60,12 @@ def concat( def concat( objs: Iterable[T_DataArray], dim: Hashable | T_Variable | T_DataArray | pd.Index | Any, - data_vars: T_DataVars = "all", - coords: ConcatOptions | list[Hashable] = "different", - compat: CompatOptions = "equals", + data_vars: T_DataVars | CombineKwargDefault = _DATA_VARS_DEFAULT, + coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault = _COORDS_DEFAULT, + compat: CompatOptions | CombineKwargDefault = _COMPAT_CONCAT_DEFAULT, positions: Iterable[Iterable[int]] | None = None, fill_value: object = dtypes.NA, - join: JoinOptions = "outer", + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, combine_attrs: CombineAttrsOptions = "override", create_index_for_new_dim: bool = True, ) -> T_DataArray: ... @@ -66,12 +74,12 @@ def concat( def concat( objs, dim, - data_vars: T_DataVars = "all", - coords="different", - compat: CompatOptions = "equals", + data_vars: T_DataVars | CombineKwargDefault = _DATA_VARS_DEFAULT, + coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault = _COORDS_DEFAULT, + compat: CompatOptions | CombineKwargDefault = _COMPAT_CONCAT_DEFAULT, positions=None, fill_value=dtypes.NA, - join: JoinOptions = "outer", + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, combine_attrs: CombineAttrsOptions = "override", create_index_for_new_dim: bool = True, ): @@ -90,7 +98,7 @@ def concat( unchanged. If dimension is provided as a Variable, DataArray or Index, its name is used as the dimension to concatenate along and the values are added as a coordinate. - data_vars : {"minimal", "different", "all"} or list of Hashable, optional + data_vars : {"minimal", "different", "all", None} or list of Hashable, optional These data variables will be concatenated together: * "minimal": Only data variables in which the dimension already appears are included. @@ -100,6 +108,8 @@ def concat( load the data payload of data variables into memory if they are not already loaded. * "all": All data variables will be concatenated. + * None: Means ``"all"`` if ``dim`` is not present in any of the ``objs``, + and ``"minimal"`` if ``dim`` is present in any of ``objs``. * list of dims: The listed data variables will be concatenated, in addition to the "minimal" data variables. @@ -107,7 +117,9 @@ def concat( coords : {"minimal", "different", "all"} or list of Hashable, optional These coordinate variables will be concatenated together: * "minimal": Only coordinates in which the dimension already appears - are included. + are included. If concatenating over a dimension _not_ + present in any of the objects, then all data variables will + be concatenated along that new dimension. * "different": Coordinates which are not equal (ignoring attributes) across all datasets are also concatenated (as well as all for which dimension already appears). Beware: this option may load the data @@ -199,7 +211,7 @@ def concat( * x (x) >> xr.concat([da.isel(x=0), da.isel(x=1)], "x") + >>> xr.concat([da.isel(x=0), da.isel(x=1)], "x", coords="minimal") Size: 48B array([[0, 1, 2], [3, 4, 5]]) @@ -207,7 +219,7 @@ def concat( * x (x) >> xr.concat([da.isel(x=0), da.isel(x=1)], "new_dim") + >>> xr.concat([da.isel(x=0), da.isel(x=1)], "new_dim", coords="all") Size: 48B array([[0, 1, 2], [3, 4, 5]]) @@ -216,7 +228,11 @@ def concat( * y (y) int64 24B 10 20 30 Dimensions without coordinates: new_dim - >>> xr.concat([da.isel(x=0), da.isel(x=1)], pd.Index([-90, -100], name="new_dim")) + >>> xr.concat( + ... [da.isel(x=0), da.isel(x=1)], + ... pd.Index([-90, -100], name="new_dim"), + ... coords="all", + ... ) Size: 48B array([[0, 1, 2], [3, 4, 5]]) @@ -255,7 +271,9 @@ def concat( except StopIteration as err: raise ValueError("must supply at least one object to concatenate") from err - if compat not in set(_VALID_COMPAT) - {"minimal"}: + if not isinstance(compat, CombineKwargDefault) and compat not in set( + _VALID_COMPAT + ) - {"minimal"}: raise ValueError( f"compat={compat!r} invalid: must be 'broadcast_equals', 'equals', 'identical', 'no_conflicts' or 'override'" ) @@ -320,26 +338,44 @@ def _calc_concat_dim_index( return dim, index -def _calc_concat_over(datasets, dim, dim_names, data_vars: T_DataVars, coords, compat): +def _calc_concat_over( + datasets: list[T_Dataset], + dim: Hashable, + all_dims: set[Hashable], + data_vars: T_DataVars | CombineKwargDefault, + coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, + compat: CompatOptions | CombineKwargDefault, +) -> tuple[set[Hashable], dict[Hashable, bool], list[int], set[Hashable]]: """ Determine which dataset variables need to be concatenated in the result, """ # variables to be concatenated concat_over = set() # variables checked for equality - equals = {} + equals: dict[Hashable, bool] = {} # skip merging these variables. # if concatenating over a dimension 'x' that is associated with an index over 2 variables, # 'x' and 'y', then we assert join="equals" on `y` and don't need to merge it. # that assertion happens in the align step prior to this function being called - skip_merge = set() + skip_merge: set[Hashable] = set() - if dim in dim_names: + if dim in all_dims: concat_over_existing_dim = True concat_over.add(dim) else: concat_over_existing_dim = False + if data_vars == "minimal" and coords == "minimal" and not concat_over_existing_dim: + raise ValueError( + "Cannot specify both data_vars='minimal' and coords='minimal' when " + "concatenating over a new dimension." + ) + + if data_vars is None or ( + isinstance(data_vars, CombineKwargDefault) and data_vars._value is None + ): + data_vars = "minimal" if concat_over_existing_dim else "all" + concat_dim_lengths = [] for ds in datasets: if concat_over_existing_dim and dim not in ds.dims and dim in ds: @@ -350,17 +386,41 @@ def _calc_concat_over(datasets, dim, dim_names, data_vars: T_DataVars, coords, c skip_merge.update(idx_vars.keys()) concat_dim_lengths.append(ds.sizes.get(dim, 1)) - def process_subset_opt(opt, subset): - if isinstance(opt, str): + def process_subset_opt( + opt: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, + subset: Literal["coords", "data_vars"], + ) -> None: + original = set(concat_over) + compat_str = ( + compat._value if isinstance(compat, CombineKwargDefault) else compat + ) + assert compat_str is not None + if isinstance(opt, str | CombineKwargDefault): if opt == "different": + if isinstance(compat, CombineKwargDefault) and compat != "override": + if not isinstance(opt, CombineKwargDefault): + emit_user_level_warning( + compat.warning_message( + "This change will result in the following ValueError: " + f"Cannot specify both {subset}='different' and compat='override'.", + recommend_set_options=False, + ), + FutureWarning, + ) + if compat == "override": raise ValueError( f"Cannot specify both {subset}='different' and compat='override'." + + ( + compat.error_message() + if isinstance(compat, CombineKwargDefault) + else "" + ) ) # all nonindexes that are not the same in each dataset for k in getattr(datasets[0], subset): if k not in concat_over: - equals[k] = None + equal = None variables = [ ds.variables[k] for ds in datasets if k in ds.variables @@ -379,19 +439,19 @@ def process_subset_opt(opt, subset): # first check without comparing values i.e. no computes for var in variables[1:]: - equals[k] = getattr(variables[0], compat)( + equal = getattr(variables[0], compat_str)( var, equiv=lazy_array_equiv ) - if equals[k] is not True: + if equal is not True: # exit early if we know these are not equal or that # equality cannot be determined i.e. one or all of # the variables wraps a numpy array break - if equals[k] is False: + if equal is False: concat_over.add(k) - elif equals[k] is None: + elif equal is None: # Compare the variable of all datasets vs. the one # of the first dataset. Perform the minimum amount of # loads in order to avoid multiple loads from disk @@ -402,7 +462,7 @@ def process_subset_opt(opt, subset): for ds_rhs in datasets[1:]: v_rhs = ds_rhs.variables[k].compute() computed.append(v_rhs) - if not getattr(v_lhs, compat)(v_rhs): + if not getattr(v_lhs, compat_str)(v_rhs): concat_over.add(k) equals[k] = False # computed variables are not to be re-computed @@ -413,7 +473,10 @@ def process_subset_opt(opt, subset): ds.variables[k].data = v.data break else: - equals[k] = True + equal = True + if TYPE_CHECKING: + assert equal is not None + equals[k] = equal elif opt == "all": concat_over.update( @@ -425,6 +488,19 @@ def process_subset_opt(opt, subset): pass else: raise ValueError(f"unexpected value for {subset}: {opt}") + + if ( + isinstance(opt, CombineKwargDefault) + and opt._value is not None + and original != concat_over + and concat_over_existing_dim + ): + warnings.append( + opt.warning_message( + "This is likely to lead to different results when multiple datasets " + "have matching variables with overlapping values.", + ) + ) else: valid_vars = tuple(getattr(datasets[0], subset)) invalid_vars = [k for k in opt if k not in valid_vars] @@ -443,8 +519,13 @@ def process_subset_opt(opt, subset): ) concat_over.update(opt) + warnings: list[str] = [] process_subset_opt(data_vars, "data_vars") process_subset_opt(coords, "coords") + + for warning in warnings: + emit_user_level_warning(warning, FutureWarning) + return concat_over, equals, concat_dim_lengths, skip_merge @@ -452,6 +533,7 @@ def process_subset_opt(opt, subset): def _parse_datasets( datasets: list[T_Dataset], ) -> tuple[ + set[Hashable], dict[Hashable, Variable], dict[Hashable, int], set[Hashable], @@ -480,20 +562,27 @@ def _parse_datasets( dim_coords[dim] = ds.coords[dim].variable dims = dims | set(ds.dims) - return dim_coords, dims_sizes, all_coord_names, data_vars, list(variables_order) + return ( + dims, + dim_coords, + dims_sizes, + all_coord_names, + data_vars, + list(variables_order), + ) def _dataset_concat( datasets: Iterable[T_Dataset], dim: str | T_Variable | T_DataArray | pd.Index, - data_vars: T_DataVars, - coords: str | list[str], - compat: CompatOptions, + data_vars: T_DataVars | CombineKwargDefault, + coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, + compat: CompatOptions | CombineKwargDefault, positions: Iterable[Iterable[int]] | None, - fill_value: Any = dtypes.NA, - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "override", - create_index_for_new_dim: bool = True, + fill_value: Any, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, + create_index_for_new_dim: bool, ) -> T_Dataset: """ Concatenate a sequence of datasets along a new or existing dimension @@ -525,10 +614,10 @@ def _dataset_concat( ) ) - dim_coords, dims_sizes, coord_names, data_names, vars_order = _parse_datasets( - datasets + all_dims, dim_coords, dims_sizes, coord_names, data_names, vars_order = ( + _parse_datasets(datasets) ) - dim_names = set(dim_coords) + indexed_dim_names = set(dim_coords) both_data_and_coords = coord_names & data_names if both_data_and_coords: @@ -542,15 +631,19 @@ def _dataset_concat( # case where concat dimension is a coordinate or data_var but not a dimension if ( dim_name in coord_names or dim_name in data_names - ) and dim_name not in dim_names: + ) and dim_name not in indexed_dim_names: datasets = [ ds.expand_dims(dim_name, create_index_for_new_dim=create_index_for_new_dim) for ds in datasets ] + all_dims.add(dim_name) + # This isn't being used any more, but keeping it up to date + # just in case we decide to use it later. + indexed_dim_names.add(dim_name) # determine which variables to concatenate concat_over, equals, concat_dim_lengths, skip_merge = _calc_concat_over( - datasets, dim_name, dim_names, data_vars, coords, compat + datasets, dim_name, all_dims, data_vars, coords, compat ) # determine which variables to merge, and then merge them according to compat @@ -725,14 +818,14 @@ def get_indexes(name): def _dataarray_concat( arrays: Iterable[T_DataArray], dim: str | T_Variable | T_DataArray | pd.Index, - data_vars: T_DataVars, - coords: str | list[str], - compat: CompatOptions, + data_vars: T_DataVars | CombineKwargDefault, + coords: ConcatOptions | Iterable[Hashable] | CombineKwargDefault, + compat: CompatOptions | CombineKwargDefault, positions: Iterable[Iterable[int]] | None, - fill_value: object = dtypes.NA, - join: JoinOptions = "outer", - combine_attrs: CombineAttrsOptions = "override", - create_index_for_new_dim: bool = True, + fill_value: object, + join: JoinOptions | CombineKwargDefault, + combine_attrs: CombineAttrsOptions, + create_index_for_new_dim: bool, ) -> T_DataArray: from xarray.core.dataarray import DataArray @@ -743,7 +836,12 @@ def _dataarray_concat( "The elements in the input list need to be either all 'Dataset's or all 'DataArray's" ) - if data_vars != "all": + # Allow passing `all` or `None` even though we always use `data_vars='all'` + # when passing off to `_dataset_concat`. + if not isinstance(data_vars, CombineKwargDefault) and data_vars not in [ + "all", + None, + ]: raise ValueError( "data_vars is not a valid argument when concatenating DataArray objects" ) @@ -761,11 +859,11 @@ def _dataarray_concat( ds = _dataset_concat( datasets, - dim, - data_vars, - coords, - compat, - positions, + dim=dim, + data_vars="all", + coords=coords, + compat=compat, + positions=positions, fill_value=fill_value, join=join, combine_attrs=combine_attrs, diff --git a/xarray/structure/merge.py b/xarray/structure/merge.py index 5c998075151..5ae70e4524f 100644 --- a/xarray/structure/merge.py +++ b/xarray/structure/merge.py @@ -15,9 +15,20 @@ filter_indexes_from_coords, indexes_equal, ) -from xarray.core.utils import Frozen, compat_dict_union, dict_equiv, equivalent +from xarray.core.utils import ( + Frozen, + compat_dict_union, + dict_equiv, + emit_user_level_warning, + equivalent, +) from xarray.core.variable import Variable, as_variable, calculate_dimensions from xarray.structure.alignment import deep_align +from xarray.util.deprecation_helpers import ( + _COMPAT_DEFAULT, + _JOIN_DEFAULT, + CombineKwargDefault, +) if TYPE_CHECKING: from xarray.core.coordinates import Coordinates @@ -89,9 +100,9 @@ class MergeError(ValueError): def unique_variable( name: Hashable, variables: list[Variable], - compat: CompatOptions = "broadcast_equals", + compat: CompatOptions | CombineKwargDefault = "broadcast_equals", equals: bool | None = None, -) -> Variable: +) -> tuple[bool | None, Variable]: """Return the unique variable from a list of variables or raise MergeError. Parameters @@ -117,7 +128,7 @@ def unique_variable( out = variables[0] if len(variables) == 1 or compat == "override": - return out + return equals, out combine_method = None @@ -131,18 +142,25 @@ def unique_variable( if compat == "no_conflicts": combine_method = "fillna" + # we return the lazy equals, so we can warn about behaviour changes + lazy_equals = equals if equals is None: + compat_str = ( + compat._value if isinstance(compat, CombineKwargDefault) else compat + ) + assert compat_str is not None # first check without comparing values i.e. no computes for var in variables[1:]: - equals = getattr(out, compat)(var, equiv=lazy_array_equiv) + equals = getattr(out, compat_str)(var, equiv=lazy_array_equiv) if equals is not True: break + lazy_equals = equals if equals is None: # now compare values with minimum number of computes out = out.compute() for var in variables[1:]: - equals = getattr(out, compat)(var) + equals = getattr(out, compat_str)(var) if not equals: break @@ -156,11 +174,11 @@ def unique_variable( for var in variables[1:]: out = getattr(out, combine_method)(var) - return out + return lazy_equals, out def _assert_compat_valid(compat): - if compat not in _VALID_COMPAT: + if not isinstance(compat, CombineKwargDefault) and compat not in _VALID_COMPAT: raise ValueError(f"compat={compat!r} invalid: must be {set(_VALID_COMPAT)}") @@ -202,7 +220,7 @@ def _assert_prioritized_valid( def merge_collected( grouped: dict[Any, list[MergeElement]], prioritized: Mapping[Any, MergeElement] | None = None, - compat: CompatOptions = "minimal", + compat: CompatOptions | CombineKwargDefault = "minimal", combine_attrs: CombineAttrsOptions = "override", equals: dict[Any, bool] | None = None, ) -> tuple[dict[Hashable, Variable], dict[Hashable, Index]]: @@ -299,9 +317,24 @@ def merge_collected( else: variables = [variable for variable, _ in elements_list] try: - merged_vars[name] = unique_variable( + equals_this_var, merged_vars[name] = unique_variable( name, variables, compat, equals.get(name) ) + # This is very likely to result in false positives, but there is no way + # to tell if the output will change without computing. + if ( + isinstance(compat, CombineKwargDefault) + and compat == "no_conflicts" + and len(variables) > 1 + and not equals_this_var + ): + emit_user_level_warning( + compat.warning_message( + "This is likely to lead to different results when " + "combining overlapping variables with the same name.", + ), + FutureWarning, + ) except MergeError: if compat != "minimal": # we need more than "minimal" compatibility (for which @@ -506,7 +539,7 @@ def coerce_pandas_values(objects: Iterable[CoercibleMapping]) -> list[DatasetLik def _get_priority_vars_and_indexes( objects: Sequence[DatasetLike], priority_arg: int | None, - compat: CompatOptions = "equals", + compat: CompatOptions | CombineKwargDefault = "equals", ) -> dict[Hashable, MergeElement]: """Extract the priority variable from a list of mappings. @@ -638,8 +671,8 @@ class _MergeResult(NamedTuple): def merge_core( objects: Iterable[CoercibleMapping], - compat: CompatOptions = "broadcast_equals", - join: JoinOptions = "outer", + compat: CompatOptions | CombineKwargDefault, + join: JoinOptions | CombineKwargDefault, combine_attrs: CombineAttrsOptions = "override", priority_arg: int | None = None, explicit_coords: Iterable[Hashable] | None = None, @@ -698,7 +731,11 @@ def merge_core( coerced = coerce_pandas_values(objects) aligned = deep_align( - coerced, join=join, copy=False, indexes=indexes, fill_value=fill_value + coerced, + join=join, + copy=False, + indexes=indexes, + fill_value=fill_value, ) for pos, obj in skip_align_objs: @@ -707,7 +744,10 @@ def merge_core( collected = collect_variables_and_indexes(aligned, indexes=indexes) prioritized = _get_priority_vars_and_indexes(aligned, priority_arg, compat=compat) variables, out_indexes = merge_collected( - collected, prioritized, compat=compat, combine_attrs=combine_attrs + collected, + prioritized, + compat=compat, + combine_attrs=combine_attrs, ) dims = calculate_dimensions(variables) @@ -738,8 +778,8 @@ def merge_core( def merge( objects: Iterable[DataArray | CoercibleMapping], - compat: CompatOptions = "no_conflicts", - join: JoinOptions = "outer", + compat: CompatOptions | CombineKwargDefault = _COMPAT_DEFAULT, + join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT, fill_value: object = dtypes.NA, combine_attrs: CombineAttrsOptions = "override", ) -> Dataset: @@ -850,7 +890,7 @@ def merge( * time (time) float64 16B 30.0 60.0 * lon (lon) float64 16B 100.0 150.0 - >>> xr.merge([x, y, z]) + >>> xr.merge([x, y, z], join="outer") Size: 256B Dimensions: (lat: 3, lon: 3, time: 2) Coordinates: @@ -862,7 +902,7 @@ def merge( var2 (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0 var3 (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0 - >>> xr.merge([x, y, z], compat="identical") + >>> xr.merge([x, y, z], compat="identical", join="outer") Size: 256B Dimensions: (lat: 3, lon: 3, time: 2) Coordinates: @@ -874,7 +914,7 @@ def merge( var2 (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0 var3 (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0 - >>> xr.merge([x, y, z], compat="equals") + >>> xr.merge([x, y, z], compat="equals", join="outer") Size: 256B Dimensions: (lat: 3, lon: 3, time: 2) Coordinates: @@ -886,7 +926,7 @@ def merge( var2 (lat, lon) float64 72B 5.0 nan 6.0 nan nan nan 7.0 nan 8.0 var3 (time, lon) float64 48B 0.0 nan 3.0 4.0 nan 9.0 - >>> xr.merge([x, y, z], compat="equals", fill_value=-999.0) + >>> xr.merge([x, y, z], compat="equals", join="outer", fill_value=-999.0) Size: 256B Dimensions: (lat: 3, lon: 3, time: 2) Coordinates: @@ -983,8 +1023,8 @@ def merge( merge_result = merge_core( dict_like_objects, - compat, - join, + compat=compat, + join=join, combine_attrs=combine_attrs, fill_value=fill_value, ) @@ -995,8 +1035,8 @@ def dataset_merge_method( dataset: Dataset, other: CoercibleMapping, overwrite_vars: Hashable | Iterable[Hashable], - compat: CompatOptions, - join: JoinOptions, + compat: CompatOptions | CombineKwargDefault, + join: JoinOptions | CombineKwargDefault, fill_value: Any, combine_attrs: CombineAttrsOptions, ) -> _MergeResult: @@ -1029,8 +1069,8 @@ def dataset_merge_method( return merge_core( objs, - compat, - join, + compat=compat, + join=join, priority_arg=priority_arg, fill_value=fill_value, combine_attrs=combine_attrs, @@ -1062,6 +1102,8 @@ def dataset_update_method(dataset: Dataset, other: CoercibleMapping) -> _MergeRe return merge_core( [dataset, other], + compat="broadcast_equals", + join="outer", priority_arg=1, indexes=dataset.xindexes, combine_attrs="override", @@ -1083,6 +1125,7 @@ def merge_data_and_coords(data_vars: DataVars, coords) -> _MergeResult: [data_vars, coords], compat="broadcast_equals", join="outer", + combine_attrs="override", explicit_coords=tuple(coords), indexes=coords.xindexes, priority_arg=1, diff --git a/xarray/tests/arrays.py b/xarray/tests/arrays.py index 31f380d2c66..4ee415619ab 100644 --- a/xarray/tests/arrays.py +++ b/xarray/tests/arrays.py @@ -3,7 +3,7 @@ """ from collections.abc import Callable, Iterable -from typing import Any +from typing import Any, Self import numpy as np @@ -126,6 +126,23 @@ def broadcast_to( return ConcatenatableArray(result) +@implements(np.full_like) +def full_like( + x: "ConcatenatableArray", /, fill_value, **kwargs +) -> "ConcatenatableArray": + """ + Broadcasts an array to a specified shape, by either manipulating chunk keys or copying chunk manifest entries. + """ + if not isinstance(x, ConcatenatableArray): + raise TypeError + return ConcatenatableArray(np.full(x.shape, fill_value=fill_value, **kwargs)) + + +@implements(np.all) +def numpy_all(x: "ConcatenatableArray", **kwargs) -> "ConcatenatableArray": + return type(x)(np.all(x._array, **kwargs)) + + class ConcatenatableArray: """Disallows loading or coercing to an index but does support concatenation / stacking.""" @@ -156,7 +173,7 @@ def __array__( ) -> np.ndarray: raise UnexpectedDataAccess("Tried accessing data") - def __getitem__(self, key) -> "ConcatenatableArray": + def __getitem__(self, key) -> Self: """Some cases of concat require supporting expanding dims by dimensions of size 1""" # see https://data-apis.org/array-api/2022.12/API_specification/indexing.html#multi-axis-indexing arr = self._array @@ -167,7 +184,10 @@ def __getitem__(self, key) -> "ConcatenatableArray": pass else: raise UnexpectedDataAccess("Tried accessing data.") - return ConcatenatableArray(arr) + return type(self)(arr) + + def __eq__(self, other: Self) -> Self: # type: ignore[override] + return type(self)(self._array == other._array) def __array_function__(self, func, types, args, kwargs) -> Any: if func not in CONCATENATABLEARRAY_HANDLED_ARRAY_FUNCTIONS: @@ -184,9 +204,15 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs) -> Any: """We have to define this in order to convince xarray that this class is a duckarray, even though we will never support ufuncs.""" return NotImplemented - def astype(self, dtype: np.dtype, /, *, copy: bool = True) -> "ConcatenatableArray": + def astype(self, dtype: np.dtype, /, *, copy: bool = True) -> Self: """Needed because xarray will call this even when it's a no-op""" if dtype != self.dtype: raise NotImplementedError() else: return self + + def __and__(self, other: Self) -> Self: + return type(self)(self._array & other._array) + + def __or__(self, other: Self) -> Self: + return type(self)(self._array | other._array) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 93329b2297d..2fe57f51d65 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -60,6 +60,7 @@ from xarray.core.types import PDDatetimeUnitOptions from xarray.core.utils import module_available from xarray.namedarray.pycompat import array_type +from xarray.structure.alignment import AlignmentError from xarray.tests import ( assert_allclose, assert_array_equal, @@ -4806,7 +4807,7 @@ class TestOpenMFDatasetWithDataVarsAndCoordsKw: var_name = "v1" @contextlib.contextmanager - def setup_files_and_datasets(self, fuzz=0): + def setup_files_and_datasets(self, *, fuzz=0, new_combine_kwargs: bool = False): ds1, ds2 = self.gen_datasets_with_common_coord_and_time() # to test join='exact' @@ -4818,7 +4819,8 @@ def setup_files_and_datasets(self, fuzz=0): ds1.to_netcdf(tmpfile1) ds2.to_netcdf(tmpfile2) - yield [tmpfile1, tmpfile2], [ds1, ds2] + with set_options(use_new_combine_kwarg_defaults=new_combine_kwargs): + yield [tmpfile1, tmpfile2], [ds1, ds2] def gen_datasets_with_common_coord_and_time(self): # create coordinate data @@ -4855,11 +4857,19 @@ def test_open_mfdataset_does_same_as_concat( if combine == "by_coords": files.reverse() with open_mfdataset( - files, data_vars=opt, combine=combine, concat_dim=concat_dim, join=join + files, + data_vars=opt, + combine=combine, + concat_dim=concat_dim, + join=join, + compat="equals", ) as ds: - ds_expect = xr.concat([ds1, ds2], data_vars=opt, dim="t", join=join) + ds_expect = xr.concat( + [ds1, ds2], data_vars=opt, dim="t", join=join, compat="equals" + ) assert_identical(ds, ds_expect) + @pytest.mark.parametrize("use_new_combine_kwarg_defaults", [True, False]) @pytest.mark.parametrize( ["combine_attrs", "attrs", "expected", "expect_error"], ( @@ -4887,7 +4897,12 @@ def test_open_mfdataset_does_same_as_concat( ), ) def test_open_mfdataset_dataset_combine_attrs( - self, combine_attrs, attrs, expected, expect_error + self, + use_new_combine_kwarg_defaults, + combine_attrs, + attrs, + expected, + expect_error, ): with self.setup_files_and_datasets() as (files, [ds1, ds2]): # Give the files an inconsistent attribute @@ -4897,22 +4912,28 @@ def test_open_mfdataset_dataset_combine_attrs( ds.close() ds.to_netcdf(f) - if expect_error: - with pytest.raises(xr.MergeError): - xr.open_mfdataset( - files, - combine="nested", - concat_dim="t", - combine_attrs=combine_attrs, - ) - else: - with xr.open_mfdataset( - files, - combine="nested", - concat_dim="t", - combine_attrs=combine_attrs, - ) as ds: - assert ds.attrs == expected + with set_options( + use_new_combine_kwarg_defaults=use_new_combine_kwarg_defaults + ): + warning: contextlib.AbstractContextManager = ( + pytest.warns(FutureWarning) + if not use_new_combine_kwarg_defaults + else contextlib.nullcontext() + ) + error: contextlib.AbstractContextManager = ( + pytest.raises(xr.MergeError) + if expect_error + else contextlib.nullcontext() + ) + with warning: + with error: + with xr.open_mfdataset( + files, + combine="nested", + concat_dim="t", + combine_attrs=combine_attrs, + ) as ds: + assert ds.attrs == expected def test_open_mfdataset_dataset_attr_by_coords(self) -> None: """ @@ -4926,14 +4947,18 @@ def test_open_mfdataset_dataset_attr_by_coords(self) -> None: ds.close() ds.to_netcdf(f) - with xr.open_mfdataset(files, combine="nested", concat_dim="t") as ds: - assert ds.test_dataset_attr == 10 + with set_options(use_new_combine_kwarg_defaults=True): + with xr.open_mfdataset(files, combine="nested", concat_dim="t") as ds: + assert ds.test_dataset_attr == 10 def test_open_mfdataset_dataarray_attr_by_coords(self) -> None: """ Case when an attribute of a member DataArray differs across the multiple files """ - with self.setup_files_and_datasets() as (files, [ds1, ds2]): + with self.setup_files_and_datasets(new_combine_kwargs=True) as ( + files, + [ds1, ds2], + ): # Give the files an inconsistent attribute for i, f in enumerate(files): ds = open_dataset(f).load() @@ -4941,30 +4966,67 @@ def test_open_mfdataset_dataarray_attr_by_coords(self) -> None: ds.close() ds.to_netcdf(f) - with xr.open_mfdataset(files, combine="nested", concat_dim="t") as ds: - assert ds["v1"].test_dataarray_attr == 0 + with xr.open_mfdataset( + files, data_vars=None, combine="nested", concat_dim="t" + ) as ds: + assert ds["v1"].test_dataarray_attr == 0 @pytest.mark.parametrize( "combine, concat_dim", [("nested", "t"), ("by_coords", None)] ) - @pytest.mark.parametrize("opt", ["all", "minimal", "different"]) + @pytest.mark.parametrize( + "kwargs", + [ + {"data_vars": "all"}, + {"data_vars": "minimal"}, + { + "data_vars": "all", + "coords": "different", + "compat": "no_conflicts", + }, # old defaults + { + "data_vars": None, + "coords": "minimal", + "compat": "override", + }, # new defaults + {"data_vars": "different", "compat": "no_conflicts"}, + {}, + ], + ) def test_open_mfdataset_exact_join_raises_error( - self, combine, concat_dim, opt + self, combine, concat_dim, kwargs ) -> None: - with self.setup_files_and_datasets(fuzz=0.1) as (files, [ds1, ds2]): + with self.setup_files_and_datasets(fuzz=0.1, new_combine_kwargs=True) as ( + files, + _, + ): if combine == "by_coords": files.reverse() + with pytest.raises( - ValueError, match=r"cannot align objects.*join.*exact.*" + ValueError, match="cannot align objects with join='exact'" ): open_mfdataset( files, - data_vars=opt, + **kwargs, combine=combine, concat_dim=concat_dim, join="exact", ) + def test_open_mfdataset_defaults_with_exact_join_warns_as_well_as_raising( + self, + ) -> None: + with self.setup_files_and_datasets(fuzz=0.1, new_combine_kwargs=True) as ( + files, + _, + ): + files.reverse() + with pytest.raises( + ValueError, match="cannot align objects with join='exact'" + ): + open_mfdataset(files, combine="by_coords") + def test_common_coord_when_datavars_all(self) -> None: opt: Final = "all" @@ -4986,7 +5048,10 @@ def test_common_coord_when_datavars_all(self) -> None: def test_common_coord_when_datavars_minimal(self) -> None: opt: Final = "minimal" - with self.setup_files_and_datasets() as (files, [ds1, ds2]): + with self.setup_files_and_datasets(new_combine_kwargs=True) as ( + files, + [ds1, ds2], + ): # open the files using data_vars option with open_mfdataset( files, data_vars=opt, combine="nested", concat_dim="t" @@ -5012,6 +5077,55 @@ def test_invalid_data_vars_value_should_fail(self) -> None: with open_mfdataset(files, coords="minimum", combine="by_coords"): pass + @pytest.mark.parametrize( + "combine, concat_dim", [("nested", "t"), ("by_coords", None)] + ) + @pytest.mark.parametrize( + "kwargs", [{"data_vars": "different"}, {"coords": "different"}] + ) + def test_open_mfdataset_warns_when_kwargs_set_to_different( + self, combine, concat_dim, kwargs + ) -> None: + with self.setup_files_and_datasets(new_combine_kwargs=True) as ( + files, + [ds1, ds2], + ): + if combine == "by_coords": + files.reverse() + with pytest.raises( + ValueError, match="Previously the default was `compat='no_conflicts'`" + ): + open_mfdataset(files, combine=combine, concat_dim=concat_dim, **kwargs) + with pytest.raises( + ValueError, match="Previously the default was `compat='equals'`" + ): + xr.concat([ds1, ds2], dim="t", **kwargs) + + with set_options(use_new_combine_kwarg_defaults=False): + expectation: contextlib.AbstractContextManager = ( + pytest.warns( + FutureWarning, + match="will change from data_vars='all'", + ) + if "data_vars" not in kwargs + else contextlib.nullcontext() + ) + + with pytest.warns( + FutureWarning, + match="will change from compat='equals'", + ): + with expectation: + ds_expect = xr.concat([ds1, ds2], dim="t", **kwargs) + with pytest.warns( + FutureWarning, match="will change from compat='no_conflicts'" + ): + with expectation: + with open_mfdataset( + files, combine=combine, concat_dim=concat_dim, **kwargs + ) as ds: + assert_identical(ds, ds_expect) + @requires_dask @requires_scipy @@ -5267,11 +5381,48 @@ def test_encoding_mfdataset(self) -> None: ds2.t.encoding["units"] = "days since 2000-01-01" ds1.to_netcdf(tmp1) ds2.to_netcdf(tmp2) - with open_mfdataset([tmp1, tmp2], combine="nested") as actual: + with open_mfdataset( + [tmp1, tmp2], combine="nested", concat_dim="t" + ) as actual: assert actual.t.encoding["units"] == original.t.encoding["units"] assert actual.t.encoding["units"] == ds1.t.encoding["units"] assert actual.t.encoding["units"] != ds2.t.encoding["units"] + def test_encoding_mfdataset_new_defaults(self) -> None: + original = Dataset( + { + "foo": ("t", np.random.randn(10)), + "t": ("t", pd.date_range(start="2010-01-01", periods=10, freq="1D")), + } + ) + original.t.encoding["units"] = "days since 2010-01-01" + + with create_tmp_file() as tmp1: + with create_tmp_file() as tmp2: + ds1 = original.isel(t=slice(5)) + ds2 = original.isel(t=slice(5, 10)) + ds1.t.encoding["units"] = "days since 2010-01-01" + ds2.t.encoding["units"] = "days since 2000-01-01" + ds1.to_netcdf(tmp1) + ds2.to_netcdf(tmp2) + + for setting in [True, False]: + with set_options(use_new_combine_kwarg_defaults=setting): + with open_mfdataset( + [tmp1, tmp2], combine="nested", concat_dim="t" + ) as old: + assert ( + old.t.encoding["units"] == original.t.encoding["units"] + ) + assert old.t.encoding["units"] == ds1.t.encoding["units"] + assert old.t.encoding["units"] != ds2.t.encoding["units"] + + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises( + AlignmentError, match="If you are intending to concatenate" + ): + open_mfdataset([tmp1, tmp2], combine="nested") + def test_preprocess_mfdataset(self) -> None: original = Dataset({"foo": ("x", np.random.randn(10))}) with create_tmp_file() as tmp: @@ -5354,25 +5505,21 @@ def test_open_and_do_math(self) -> None: actual = 1.0 * ds assert_allclose(original, actual, decode_bytes=False) - def test_open_mfdataset_concat_dim_none(self) -> None: - with create_tmp_file() as tmp1: - with create_tmp_file() as tmp2: - data = Dataset({"x": 0}) - data.to_netcdf(tmp1) - Dataset({"x": np.nan}).to_netcdf(tmp2) - with open_mfdataset( - [tmp1, tmp2], concat_dim=None, combine="nested" - ) as actual: - assert_identical(data, actual) - - def test_open_mfdataset_concat_dim_default_none(self) -> None: - with create_tmp_file() as tmp1: - with create_tmp_file() as tmp2: - data = Dataset({"x": 0}) - data.to_netcdf(tmp1) - Dataset({"x": np.nan}).to_netcdf(tmp2) - with open_mfdataset([tmp1, tmp2], combine="nested") as actual: - assert_identical(data, actual) + @pytest.mark.parametrize( + "kwargs", + [pytest.param({"concat_dim": None}, id="none"), pytest.param({}, id="default")], + ) + def test_open_mfdataset_concat_dim(self, kwargs) -> None: + with set_options(use_new_combine_kwarg_defaults=True): + with create_tmp_file() as tmp1: + with create_tmp_file() as tmp2: + data = Dataset({"x": 0}) + data.to_netcdf(tmp1) + Dataset({"x": np.nan}).to_netcdf(tmp2) + with open_mfdataset( + [tmp1, tmp2], **kwargs, combine="nested" + ) as actual: + assert_identical(data, actual) def test_open_dataset(self) -> None: original = Dataset({"foo": ("x", np.random.randn(10))}) @@ -5399,7 +5546,9 @@ def test_open_single_dataset(self) -> None: ) with create_tmp_file() as tmp: original.to_netcdf(tmp) - with open_mfdataset([tmp], concat_dim=dim, combine="nested") as actual: + with open_mfdataset( + [tmp], concat_dim=dim, data_vars="all", combine="nested" + ) as actual: assert_identical(expected, actual) def test_open_multi_dataset(self) -> None: @@ -5423,7 +5572,7 @@ def test_open_multi_dataset(self) -> None: original.to_netcdf(tmp1) original.to_netcdf(tmp2) with open_mfdataset( - [tmp1, tmp2], concat_dim=dim, combine="nested" + [tmp1, tmp2], concat_dim=dim, data_vars="all", combine="nested" ) as actual: assert_identical(expected, actual) @@ -6960,7 +7109,8 @@ def test_h5netcdf_storage_options() -> None: files, engine="h5netcdf", concat_dim="time", + data_vars="all", combine="nested", storage_options={"skip_instance_cache": False}, ) as ds: - assert_identical(xr.concat([ds1, ds2], dim="time"), ds) + assert_identical(xr.concat([ds1, ds2], dim="time", data_vars="all"), ds) diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index 40d63ed6981..3b4ad097795 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -13,6 +13,7 @@ combine_nested, concat, merge, + set_options, ) from xarray.core import dtypes from xarray.structure.combine import ( @@ -279,8 +280,10 @@ def test_get_new_tile_ids(self, create_combined_ids): class TestCombineND: - @pytest.mark.parametrize("concat_dim", ["dim1", "new_dim"]) - def test_concat_once(self, create_combined_ids, concat_dim): + @pytest.mark.parametrize( + "concat_dim, kwargs", [("dim1", {}), ("new_dim", {"data_vars": "all"})] + ) + def test_concat_once(self, create_combined_ids, concat_dim, kwargs): shape = (2,) combined_ids = create_combined_ids(shape) ds = create_test_data @@ -290,9 +293,12 @@ def test_concat_once(self, create_combined_ids, concat_dim): data_vars="all", coords="different", compat="no_conflicts", + fill_value=dtypes.NA, + join="outer", + combine_attrs="drop", ) - expected_ds = concat([ds(0), ds(1)], dim=concat_dim) + expected_ds = concat([ds(0), ds(1)], dim=concat_dim, **kwargs) assert_combined_tile_ids_equal(result, {(): expected_ds}) def test_concat_only_first_dim(self, create_combined_ids): @@ -304,6 +310,9 @@ def test_concat_only_first_dim(self, create_combined_ids): data_vars="all", coords="different", compat="no_conflicts", + fill_value=dtypes.NA, + join="outer", + combine_attrs="drop", ) ds = create_test_data @@ -315,17 +324,28 @@ def test_concat_only_first_dim(self, create_combined_ids): assert_combined_tile_ids_equal(result, expected) - @pytest.mark.parametrize("concat_dim", ["dim1", "new_dim"]) - def test_concat_twice(self, create_combined_ids, concat_dim): + @pytest.mark.parametrize( + "concat_dim, kwargs", [("dim1", {}), ("new_dim", {"data_vars": "all"})] + ) + def test_concat_twice(self, create_combined_ids, concat_dim, kwargs): shape = (2, 3) combined_ids = create_combined_ids(shape) - result = _combine_nd(combined_ids, concat_dims=["dim1", concat_dim]) + result = _combine_nd( + combined_ids, + concat_dims=["dim1", concat_dim], + data_vars="all", + coords="different", + compat="no_conflicts", + fill_value=dtypes.NA, + join="outer", + combine_attrs="drop", + ) ds = create_test_data partway1 = concat([ds(0), ds(3)], dim="dim1") partway2 = concat([ds(1), ds(4)], dim="dim1") partway3 = concat([ds(2), ds(5)], dim="dim1") - expected = concat([partway1, partway2, partway3], dim=concat_dim) + expected = concat([partway1, partway2, partway3], **kwargs, dim=concat_dim) assert_equal(result, expected) @@ -417,7 +437,7 @@ def test_nested_concat_along_new_dim(self): Dataset({"a": ("x", [20]), "x": [0]}), ] expected = Dataset({"a": (("t", "x"), [[10], [20]]), "x": [0]}) - actual = combine_nested(objs, concat_dim="t") + actual = combine_nested(objs, data_vars="all", concat_dim="t") assert_identical(expected, actual) # Same but with a DataArray as new dim, see GH #1988 and #2647 @@ -425,42 +445,60 @@ def test_nested_concat_along_new_dim(self): expected = Dataset( {"a": (("baz", "x"), [[10], [20]]), "x": [0], "baz": [100, 150]} ) - actual = combine_nested(objs, concat_dim=dim) + actual = combine_nested(objs, data_vars="all", concat_dim=dim) assert_identical(expected, actual) - def test_nested_merge(self): + def test_nested_merge_with_self(self): data = Dataset({"x": 0}) actual = combine_nested([data, data, data], concat_dim=None) assert_identical(data, actual) + def test_nested_merge_with_overlapping_values(self): ds1 = Dataset({"a": ("x", [1, 2]), "x": [0, 1]}) ds2 = Dataset({"a": ("x", [2, 3]), "x": [1, 2]}) expected = Dataset({"a": ("x", [1, 2, 3]), "x": [0, 1, 2]}) - actual = combine_nested([ds1, ds2], concat_dim=None) + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + actual = combine_nested([ds1, ds2], join="outer", concat_dim=None) assert_identical(expected, actual) - actual = combine_nested([ds1, ds2], concat_dim=[None]) + actual = combine_nested( + [ds1, ds2], join="outer", compat="no_conflicts", concat_dim=None + ) + assert_identical(expected, actual) + actual = combine_nested( + [ds1, ds2], join="outer", compat="no_conflicts", concat_dim=[None] + ) assert_identical(expected, actual) + def test_nested_merge_with_nan_no_conflicts(self): tmp1 = Dataset({"x": 0}) tmp2 = Dataset({"x": np.nan}) - actual = combine_nested([tmp1, tmp2], concat_dim=None) + actual = combine_nested([tmp1, tmp2], compat="no_conflicts", concat_dim=None) assert_identical(tmp1, actual) - actual = combine_nested([tmp1, tmp2], concat_dim=[None]) + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + combine_nested([tmp1, tmp2], concat_dim=None) + actual = combine_nested([tmp1, tmp2], compat="no_conflicts", concat_dim=[None]) assert_identical(tmp1, actual) - # Single object, with a concat_dim explicitly provided + def test_nested_merge_with_concat_dim_explicitly_provided(self): # Test the issue reported in GH #1988 objs = [Dataset({"x": 0, "y": 1})] dim = DataArray([100], name="baz", dims="baz") - actual = combine_nested(objs, concat_dim=[dim]) + actual = combine_nested(objs, concat_dim=[dim], data_vars="all") expected = Dataset({"x": ("baz", [0]), "y": ("baz", [1])}, {"baz": [100]}) assert_identical(expected, actual) + def test_nested_merge_with_non_scalars(self): # Just making sure that auto_combine is doing what is # expected for non-scalar values, too. objs = [Dataset({"x": ("z", [0, 1]), "y": ("z", [1, 2])})] dim = DataArray([100], name="baz", dims="baz") - actual = combine_nested(objs, concat_dim=[dim]) + actual = combine_nested(objs, concat_dim=[dim], data_vars="all") expected = Dataset( {"x": (("baz", "z"), [[0, 1]]), "y": (("baz", "z"), [[1, 2]])}, {"baz": [100]}, @@ -510,10 +548,14 @@ def test_auto_combine_2d(self): partway1 = concat([ds(0), ds(3)], dim="dim1") partway2 = concat([ds(1), ds(4)], dim="dim1") partway3 = concat([ds(2), ds(5)], dim="dim1") - expected = concat([partway1, partway2, partway3], dim="dim2") + expected = concat([partway1, partway2, partway3], data_vars="all", dim="dim2") datasets = [[ds(0), ds(1), ds(2)], [ds(3), ds(4), ds(5)]] - result = combine_nested(datasets, concat_dim=["dim1", "dim2"]) + result = combine_nested( + datasets, + data_vars="all", + concat_dim=["dim1", "dim2"], + ) assert_equal(result, expected) def test_auto_combine_2d_combine_attrs_kwarg(self): @@ -522,7 +564,7 @@ def test_auto_combine_2d_combine_attrs_kwarg(self): partway1 = concat([ds(0), ds(3)], dim="dim1") partway2 = concat([ds(1), ds(4)], dim="dim1") partway3 = concat([ds(2), ds(5)], dim="dim1") - expected = concat([partway1, partway2, partway3], dim="dim2") + expected = concat([partway1, partway2, partway3], data_vars="all", dim="dim2") expected_dict = {} expected_dict["drop"] = expected.copy(deep=True) @@ -553,12 +595,18 @@ def test_auto_combine_2d_combine_attrs_kwarg(self): with pytest.raises(ValueError, match=r"combine_attrs='identical'"): result = combine_nested( - datasets, concat_dim=["dim1", "dim2"], combine_attrs="identical" + datasets, + concat_dim=["dim1", "dim2"], + data_vars="all", + combine_attrs="identical", ) for combine_attrs, expected in expected_dict.items(): result = combine_nested( - datasets, concat_dim=["dim1", "dim2"], combine_attrs=combine_attrs + datasets, + concat_dim=["dim1", "dim2"], + data_vars="all", + combine_attrs=combine_attrs, ) assert_identical(result, expected) @@ -572,7 +620,7 @@ def test_combine_nested_missing_data_new_dim(self): expected = Dataset( {"a": (("t", "x"), [[np.nan, 2, 3], [1, 2, np.nan]])}, {"x": [0, 1, 2]} ) - actual = combine_nested(datasets, concat_dim="t") + actual = combine_nested(datasets, data_vars="all", join="outer", concat_dim="t") assert_identical(expected, actual) def test_invalid_hypercube_input(self): @@ -650,7 +698,13 @@ def test_combine_nested_fill_value(self, fill_value): }, {"x": [0, 1, 2]}, ) - actual = combine_nested(datasets, concat_dim="t", fill_value=fill_value) + actual = combine_nested( + datasets, + concat_dim="t", + data_vars="all", + join="outer", + fill_value=fill_value, + ) assert_identical(expected, actual) def test_combine_nested_unnamed_data_arrays(self): @@ -710,26 +764,30 @@ def test_combine_by_coords(self): expected = Dataset({"x": [0, 1, 2]}) assert_identical(expected, actual) + def test_combine_by_coords_handles_non_sorted_variables(self): # ensure auto_combine handles non-sorted variables objs = [ Dataset({"x": ("a", [0]), "y": ("a", [0]), "a": [0]}), Dataset({"x": ("a", [1]), "y": ("a", [1]), "a": [1]}), ] - actual = combine_by_coords(objs) + actual = combine_by_coords(objs, join="outer") expected = Dataset({"x": ("a", [0, 1]), "y": ("a", [0, 1]), "a": [0, 1]}) assert_identical(expected, actual) + def test_combine_by_coords_multiple_variables(self): objs = [Dataset({"x": [0], "y": [0]}), Dataset({"y": [1], "x": [1]})] - actual = combine_by_coords(objs) + actual = combine_by_coords(objs, join="outer") expected = Dataset({"x": [0, 1], "y": [0, 1]}) assert_equal(actual, expected) + def test_combine_by_coords_for_scalar_variables(self): objs = [Dataset({"x": 0}), Dataset({"x": 1})] with pytest.raises( ValueError, match=r"Could not find any dimension coordinates" ): combine_by_coords(objs) + def test_combine_by_coords_requires_coord_or_index(self): objs = [Dataset({"x": [0], "y": [0]}), Dataset({"x": [0]})] with pytest.raises( ValueError, @@ -959,10 +1017,14 @@ def test_combine_by_coords_combine_attrs_variables( def test_infer_order_from_coords(self): data = create_test_data() objs = [data.isel(dim2=slice(4, 9)), data.isel(dim2=slice(4))] - actual = combine_by_coords(objs) + actual = combine_by_coords(objs, data_vars="all") expected = data assert expected.broadcast_equals(actual) + with set_options(use_new_combine_kwarg_defaults=True): + actual = combine_by_coords(objs) + assert_identical(actual, expected) + def test_combine_leaving_bystander_dimensions(self): # Check non-monotonic bystander dimension coord doesn't raise # ValueError on combine (https://github.com/pydata/xarray/issues/3150) @@ -997,7 +1059,7 @@ def test_combine_by_coords_previously_failed(self): Dataset({"a": ("x", [1]), "x": [1]}), ] expected = Dataset({"a": ("x", [0, 1]), "b": ("x", [0, np.nan])}, {"x": [0, 1]}) - actual = combine_by_coords(datasets) + actual = combine_by_coords(datasets, join="outer") assert_identical(expected, actual) def test_combine_by_coords_still_fails(self): @@ -1014,7 +1076,7 @@ def test_combine_by_coords_no_concat(self): assert_identical(expected, actual) objs = [Dataset({"x": 0, "y": 1}), Dataset({"y": np.nan, "z": 2})] - actual = combine_by_coords(objs) + actual = combine_by_coords(objs, compat="no_conflicts") expected = Dataset({"x": 0, "y": 1, "z": 2}) assert_identical(expected, actual) @@ -1032,7 +1094,7 @@ def test_combine_by_coords_incomplete_hypercube(self): x1 = Dataset({"a": (("y", "x"), [[1]])}, coords={"y": [0], "x": [0]}) x2 = Dataset({"a": (("y", "x"), [[1]])}, coords={"y": [1], "x": [0]}) x3 = Dataset({"a": (("y", "x"), [[1]])}, coords={"y": [0], "x": [1]}) - actual = combine_by_coords([x1, x2, x3]) + actual = combine_by_coords([x1, x2, x3], join="outer") expected = Dataset( {"a": (("y", "x"), [[1, 1], [1, np.nan]])}, coords={"y": [0, 1], "x": [0, 1]}, @@ -1040,8 +1102,10 @@ def test_combine_by_coords_incomplete_hypercube(self): assert_identical(expected, actual) # test that this fails if fill_value is None - with pytest.raises(ValueError): - combine_by_coords([x1, x2, x3], fill_value=None) + with pytest.raises( + ValueError, match="supplied objects do not form a hypercube" + ): + combine_by_coords([x1, x2, x3], join="outer", fill_value=None) def test_combine_by_coords_override_order(self) -> None: # regression test for https://github.com/pydata/xarray/issues/8828 @@ -1111,7 +1175,7 @@ def test_combine_by_coords_all_named_dataarrays(self): named_da1 = DataArray(name="a", data=[1.0, 2.0], coords={"x": [0, 1]}, dims="x") named_da2 = DataArray(name="b", data=[3.0, 4.0], coords={"x": [2, 3]}, dims="x") - actual = combine_by_coords([named_da1, named_da2]) + actual = combine_by_coords([named_da1, named_da2], join="outer") expected = Dataset( { "a": DataArray(data=[1.0, 2.0], coords={"x": [0, 1]}, dims="x"), @@ -1124,11 +1188,132 @@ def test_combine_by_coords_all_dataarrays_with_the_same_name(self): named_da1 = DataArray(name="a", data=[1.0, 2.0], coords={"x": [0, 1]}, dims="x") named_da2 = DataArray(name="a", data=[3.0, 4.0], coords={"x": [2, 3]}, dims="x") - actual = combine_by_coords([named_da1, named_da2]) - expected = merge([named_da1, named_da2]) + actual = combine_by_coords([named_da1, named_da2], join="outer") + expected = merge([named_da1, named_da2], compat="no_conflicts", join="outer") assert_identical(expected, actual) +class TestNewDefaults: + def test_concat_along_existing_dim(self): + concat_dim = "dim1" + ds = create_test_data + with set_options(use_new_combine_kwarg_defaults=False): + old = concat([ds(0), ds(1)], dim=concat_dim) + with set_options(use_new_combine_kwarg_defaults=True): + new = concat([ds(0), ds(1)], dim=concat_dim) + assert_identical(old, new) + + def test_concat_along_new_dim(self): + concat_dim = "new_dim" + ds = create_test_data + with set_options(use_new_combine_kwarg_defaults=False): + old = concat([ds(0), ds(1)], dim=concat_dim) + with set_options(use_new_combine_kwarg_defaults=True): + new = concat([ds(0), ds(1)], dim=concat_dim) + + assert concat_dim in old.dims + assert concat_dim in new.dims + + def test_nested_merge_with_overlapping_values(self): + ds1 = Dataset({"a": ("x", [1, 2]), "x": [0, 1]}) + ds2 = Dataset({"a": ("x", [2, 3]), "x": [1, 2]}) + expected = Dataset({"a": ("x", [1, 2, 3]), "x": [0, 1, 2]}) + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, match="will change from join='outer' to join='exact'" + ): + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + old = combine_nested([ds1, ds2], concat_dim=None) + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises(ValueError, match="might be related to new default"): + combine_nested([ds1, ds2], concat_dim=None) + + assert_identical(old, expected) + + def test_nested_merge_with_nan_order_matters(self): + ds1 = Dataset({"x": 0}) + ds2 = Dataset({"x": np.nan}) + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + old = combine_nested([ds1, ds2], concat_dim=None) + with set_options(use_new_combine_kwarg_defaults=True): + new = combine_nested([ds1, ds2], concat_dim=None) + + assert_identical(ds1, old) + assert_identical(old, new) + + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + old = combine_nested([ds2, ds1], concat_dim=None) + with set_options(use_new_combine_kwarg_defaults=True): + new = combine_nested([ds2, ds1], concat_dim=None) + + assert_identical(ds1, old) + with pytest.raises(AssertionError): + assert_identical(old, new) + + def test_nested_merge_with_concat_dim_explicitly_provided(self): + # Test the issue reported in GH #1988 + objs = [Dataset({"x": 0, "y": 1})] + dim = DataArray([100], name="baz", dims="baz") + expected = Dataset({"x": ("baz", [0]), "y": ("baz", [1])}, {"baz": [100]}) + + with set_options(use_new_combine_kwarg_defaults=False): + old = combine_nested(objs, concat_dim=dim) + with set_options(use_new_combine_kwarg_defaults=True): + new = combine_nested(objs, concat_dim=dim) + + assert_identical(expected, old) + assert_identical(old, new) + + def test_combine_nested_missing_data_new_dim(self): + # Your data includes "time" and "station" dimensions, and each year's + # data has a different set of stations. + datasets = [ + Dataset({"a": ("x", [2, 3]), "x": [1, 2]}), + Dataset({"a": ("x", [1, 2]), "x": [0, 1]}), + ] + expected = Dataset( + {"a": (("t", "x"), [[np.nan, 2, 3], [1, 2, np.nan]])}, {"x": [0, 1, 2]} + ) + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, match="will change from join='outer' to join='exact'" + ): + old = combine_nested(datasets, concat_dim="t") + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises(ValueError, match="might be related to new default"): + combine_nested(datasets, concat_dim="t") + new = combine_nested(datasets, concat_dim="t", join="outer") + + assert_identical(expected, old) + assert_identical(expected, new) + + def test_combine_by_coords_multiple_variables(self): + objs = [Dataset({"x": [0], "y": [0]}), Dataset({"y": [1], "x": [1]})] + expected = Dataset({"x": [0, 1], "y": [0, 1]}) + + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, match="will change from join='outer' to join='exact'" + ): + old = combine_by_coords(objs) + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises(ValueError, match="might be related to new default"): + combine_by_coords(objs) + + assert_identical(old, expected) + + @requires_cftime def test_combine_by_coords_distant_cftime_dates(): # Regression test for https://github.com/pydata/xarray/issues/3535 diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index 668a48a9c24..2c3692863f0 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -1,6 +1,7 @@ from __future__ import annotations from collections.abc import Callable +from contextlib import AbstractContextManager, nullcontext from copy import deepcopy from typing import TYPE_CHECKING, Any, Literal @@ -8,7 +9,7 @@ import pandas as pd import pytest -from xarray import AlignmentError, DataArray, Dataset, Variable, concat +from xarray import AlignmentError, DataArray, Dataset, Variable, concat, set_options from xarray.core import dtypes, types from xarray.core.coordinates import Coordinates from xarray.core.indexes import PandasIndex @@ -135,9 +136,9 @@ def test_concat_compat() -> None: for var in ["has_x", "no_x_y"]: assert "y" not in result[var].dims and "y" not in result[var].coords with pytest.raises(ValueError, match=r"'q' not present in all datasets"): - concat([ds1, ds2], dim="q") + concat([ds1, ds2], dim="q", data_vars="all", join="outer") with pytest.raises(ValueError, match=r"'q' not present in all datasets"): - concat([ds2, ds1], dim="q") + concat([ds2, ds1], dim="q", data_vars="all", join="outer") def test_concat_missing_var() -> None: @@ -217,8 +218,12 @@ def test_concat_second_empty() -> None: actual = concat([ds1, ds2], dim="y", coords="all") assert_identical(actual, expected) + +def test_concat_second_empty_with_scalar_data_var_only_on_first() -> None: # Check concatenating scalar data_var only present in ds1 - ds1["b"] = 0.1 + ds1 = Dataset(data_vars={"a": ("y", [0.1]), "b": 0.1}, coords={"x": 0.1}) + ds2 = Dataset(coords={"x": 0.1}) + expected = Dataset( data_vars={"a": ("y", [0.1, np.nan]), "b": ("y", [0.1, np.nan])}, coords={"x": ("y", [0.1, 0.1])}, @@ -229,7 +234,9 @@ def test_concat_second_empty() -> None: expected = Dataset( data_vars={"a": ("y", [0.1, np.nan]), "b": 0.1}, coords={"x": 0.1} ) - actual = concat([ds1, ds2], dim="y", coords="different", data_vars="different") + actual = concat( + [ds1, ds2], dim="y", coords="different", data_vars="different", compat="equals" + ) assert_identical(actual, expected) @@ -266,10 +273,13 @@ def test_concat_multiple_datasets_missing_vars(include_day: bool) -> None: "cloud_cover", ] + # must specify if concat_dim='day' is not part of the vars + kwargs = {"data_vars": "all"} if not include_day else {} + datasets = create_concat_datasets( len(vars_to_drop), seed=123, include_day=include_day ) - expected = concat(datasets, dim="day") + expected = concat(datasets, dim="day", **kwargs) # type: ignore[call-overload] for i, name in enumerate(vars_to_drop): if include_day: @@ -282,8 +292,7 @@ def test_concat_multiple_datasets_missing_vars(include_day: bool) -> None: ds.drop_vars(varname) for ds, varname in zip(datasets, vars_to_drop, strict=True) ] - - actual = concat(datasets, dim="day") + actual = concat(datasets, dim="day", **kwargs) # type: ignore[call-overload] assert list(actual.data_vars.keys()) == [ "pressure", @@ -493,8 +502,8 @@ def rectify_dim_order(self, data: Dataset, dataset) -> Dataset: "dim,data", [["dim1", True], ["dim2", False]], indirect=["data"] ) def test_concat_simple(self, data: Dataset, dim, coords) -> None: - datasets = [g for _, g in data.groupby(dim, squeeze=False)] - assert_identical(data, concat(datasets, dim, coords=coords)) + datasets = [g for _, g in data.groupby(dim)] + assert_identical(data, concat(datasets, dim, coords=coords, compat="equals")) def test_concat_merge_variables_present_in_some_datasets( self, data: Dataset @@ -515,7 +524,7 @@ def test_concat_merge_variables_present_in_some_datasets( assert_identical(expected, actual) # expand foo - actual = concat([data0, data1], "dim1") + actual = concat([data0, data1], "dim1", data_vars="all") foo = np.ones((8, 10), dtype=data1.foo.dtype) * np.nan foo[3:] = data1.foo.values[None, ...] expected = data.copy().assign(foo=(["dim1", "bar"], foo)) @@ -537,9 +546,11 @@ def test_concat_coords_kwarg( data = data.copy(deep=True) # make sure the coords argument behaves as expected data.coords["extra"] = ("dim4", np.arange(3)) - datasets = [g.squeeze() for _, g in data.groupby(dim, squeeze=False)] + datasets = [g for _, g in data.groupby(dim)] - actual = concat(datasets, data[dim], coords=coords) + actual = concat( + datasets, data[dim], coords=coords, data_vars="all", compat="equals" + ) if coords == "all": expected = np.array([data["extra"].values for _ in range(data.sizes[dim])]) assert_array_equal(actual["extra"].values, expected) @@ -571,41 +582,72 @@ def test_concat_data_vars_typing(self) -> None: actual = concat(objs, dim="x", data_vars="minimal") assert_identical(data, actual) - def test_concat_data_vars(self) -> None: + @pytest.mark.parametrize("data_vars", ["minimal", "different", "all", [], ["foo"]]) + def test_concat_data_vars(self, data_vars) -> None: data = Dataset({"foo": ("x", np.random.randn(10))}) objs: list[Dataset] = [data.isel(x=slice(5)), data.isel(x=slice(5, None))] - for data_vars in ["minimal", "different", "all", [], ["foo"]]: - actual = concat(objs, dim="x", data_vars=data_vars) - assert_identical(data, actual) + actual = concat(objs, dim="x", data_vars=data_vars, compat="equals") + assert_identical(data, actual) - def test_concat_coords(self): - # TODO: annotating this func fails + @pytest.mark.parametrize("coords", ["different", "all", ["c"]]) + def test_concat_coords(self, coords) -> None: data = Dataset({"foo": ("x", np.random.randn(10))}) expected = data.assign_coords(c=("x", [0] * 5 + [1] * 5)) objs = [ data.isel(x=slice(5)).assign_coords(c=0), data.isel(x=slice(5, None)).assign_coords(c=1), ] - for coords in ["different", "all", ["c"]]: + if coords == "different": + actual = concat(objs, dim="x", coords=coords, compat="equals") + else: actual = concat(objs, dim="x", coords=coords) - assert_identical(expected, actual) - for coords in ["minimal", []]: - with pytest.raises(merge.MergeError, match="conflicting values"): - concat(objs, dim="x", coords=coords) + assert_identical(expected, actual) - def test_concat_constant_index(self): - # TODO: annotating this func fails + @pytest.mark.parametrize("coords", ["minimal", []]) + def test_concat_coords_raises_merge_error(self, coords) -> None: + data = Dataset({"foo": ("x", np.random.randn(10))}) + objs = [ + data.isel(x=slice(5)).assign_coords(c=0), + data.isel(x=slice(5, None)).assign_coords(c=1), + ] + with pytest.raises(merge.MergeError, match="conflicting values"): + concat(objs, dim="x", coords=coords, compat="equals") + + @pytest.mark.parametrize("data_vars", ["different", "all", ["foo"]]) + def test_concat_constant_index(self, data_vars) -> None: # GH425 ds1 = Dataset({"foo": 1.5}, {"y": 1}) ds2 = Dataset({"foo": 2.5}, {"y": 1}) expected = Dataset({"foo": ("y", [1.5, 2.5]), "y": [1, 1]}) - for mode in ["different", "all", ["foo"]]: - actual = concat([ds1, ds2], "y", data_vars=mode) - assert_identical(expected, actual) - with pytest.raises(merge.MergeError, match="conflicting values"): - # previously dim="y", and raised error which makes no sense. - # "foo" has dimension "y" so minimal should concatenate it? - concat([ds1, ds2], "new_dim", data_vars="minimal") + if data_vars == "different": + actual = concat([ds1, ds2], "y", data_vars=data_vars, compat="equals") + else: + actual = concat([ds1, ds2], "y", data_vars=data_vars) + assert_identical(expected, actual) + + def test_concat_constant_index_None(self) -> None: + ds1 = Dataset({"foo": 1.5}, {"y": 1}) + ds2 = Dataset({"foo": 2.5}, {"y": 1}) + actual = concat([ds1, ds2], "new_dim", data_vars=None, compat="equals") + expected = Dataset( + {"foo": ("new_dim", [1.5, 2.5])}, + coords={"y": 1}, + ) + assert_identical(actual, expected) + + def test_concat_constant_index_minimal(self) -> None: + ds1 = Dataset({"foo": 1.5}, {"y": 1}) + ds2 = Dataset({"foo": 2.5}, {"y": 1}) + + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.raises(merge.MergeError, match="conflicting values"): + concat([ds1, ds2], dim="new_dim", data_vars="minimal") + + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises( + ValueError, match="data_vars='minimal' and coords='minimal'" + ): + concat([ds1, ds2], dim="new_dim", data_vars="minimal") def test_concat_size0(self) -> None: data = create_test_data() @@ -619,7 +661,7 @@ def test_concat_size0(self) -> None: def test_concat_autoalign(self) -> None: ds1 = Dataset({"foo": DataArray([1, 2], coords=[("x", [1, 2])])}) ds2 = Dataset({"foo": DataArray([1, 2], coords=[("x", [1, 3])])}) - actual = concat([ds1, ds2], "y") + actual = concat([ds1, ds2], "y", data_vars="all", join="outer") expected = Dataset( { "foo": DataArray( @@ -631,8 +673,7 @@ def test_concat_autoalign(self) -> None: ) assert_identical(expected, actual) - def test_concat_errors(self): - # TODO: annotating this func fails + def test_concat_errors(self) -> None: data = create_test_data() split_data = [data.isel(dim1=slice(3)), data.isel(dim1=slice(3, None))] @@ -662,7 +703,7 @@ def test_concat_errors(self): assert_identical(data, concat([data0, data1], "dim1", compat="equals")) with pytest.raises(ValueError, match=r"compat.* invalid"): - concat(split_data, "dim1", compat="foobar") + concat(split_data, "dim1", compat="foobar") # type: ignore[call-overload] with pytest.raises(ValueError, match=r"compat.* invalid"): concat(split_data, "dim1", compat="minimal") @@ -847,8 +888,7 @@ def test_concat_combine_attrs_kwarg_variables( assert_identical(actual, expected) - def test_concat_promote_shape(self) -> None: - # mixed dims within variables + def test_concat_promote_shape_with_mixed_dims_within_variables(self) -> None: objs = [Dataset({}, {"x": 0}), Dataset({"x": [1]})] actual = concat(objs, "x") expected = Dataset({"x": [0, 1]}) @@ -858,25 +898,30 @@ def test_concat_promote_shape(self) -> None: actual = concat(objs, "x") assert_identical(actual, expected) - # mixed dims between variables + def test_concat_promote_shape_with_mixed_dims_between_variables(self) -> None: objs = [Dataset({"x": [2], "y": 3}), Dataset({"x": [4], "y": 5})] - actual = concat(objs, "x") + actual = concat(objs, "x", data_vars="all") expected = Dataset({"x": [2, 4], "y": ("x", [3, 5])}) assert_identical(actual, expected) - # mixed dims in coord variable + def test_concat_promote_shape_with_mixed_dims_in_coord_variable(self) -> None: objs = [Dataset({"x": [0]}, {"y": -1}), Dataset({"x": [1]}, {"y": ("x", [-2])})] actual = concat(objs, "x") expected = Dataset({"x": [0, 1]}, {"y": ("x", [-1, -2])}) assert_identical(actual, expected) - # scalars with mixed lengths along concat dim -- values should repeat + def test_concat_promote_shape_for_scalars_with_mixed_lengths_along_concat_dim( + self, + ) -> None: + # values should repeat objs = [Dataset({"x": [0]}, {"y": -1}), Dataset({"x": [1, 2]}, {"y": -2})] - actual = concat(objs, "x") + actual = concat(objs, "x", coords="different", compat="equals") expected = Dataset({"x": [0, 1, 2]}, {"y": ("x", [-1, -2, -2])}) assert_identical(actual, expected) + actual = concat(objs, "x", coords="all") + assert_identical(actual, expected) - # broadcast 1d x 1d -> 2d + def test_concat_promote_shape_broadcast_1d_x_1d_goes_to_2d(self) -> None: objs = [ Dataset({"z": ("x", [-1])}, {"x": [0], "y": [0]}), Dataset({"z": ("y", [1])}, {"x": [1], "y": [0]}), @@ -885,6 +930,7 @@ def test_concat_promote_shape(self) -> None: expected = Dataset({"z": (("x", "y"), [[-1], [1]])}, {"x": [0, 1], "y": [0]}) assert_identical(actual, expected) + def test_concat_promote_shape_with_scalar_coordinates(self) -> None: # regression GH6384 objs = [ Dataset({}, {"x": pd.Interval(-1, 0, closed="right")}), @@ -901,6 +947,7 @@ def test_concat_promote_shape(self) -> None: ) assert_identical(actual, expected) + def test_concat_promote_shape_with_coordinates_of_particular_dtypes(self) -> None: # regression GH6416 (coord dtype) and GH6434 time_data1 = np.array(["2022-01-01", "2022-02-01"], dtype="datetime64[ns]") time_data2 = np.array("2022-03-01", dtype="datetime64[ns]") @@ -934,14 +981,14 @@ def test_concat_dim_is_variable(self) -> None: objs = [Dataset({"x": 0}), Dataset({"x": 1})] coord = Variable("y", [3, 4], attrs={"foo": "bar"}) expected = Dataset({"x": ("y", [0, 1]), "y": coord}) - actual = concat(objs, coord) + actual = concat(objs, coord, data_vars="all") assert_identical(actual, expected) def test_concat_dim_is_dataarray(self) -> None: objs = [Dataset({"x": 0}), Dataset({"x": 1})] coord = DataArray([3, 4], dims="y", attrs={"foo": "bar"}) expected = Dataset({"x": ("y", [0, 1]), "y": coord}) - actual = concat(objs, coord) + actual = concat(objs, coord, data_vars="all") assert_identical(actual, expected) def test_concat_multiindex(self) -> None: @@ -968,8 +1015,8 @@ def test_concat_along_new_dim_multiindex(self) -> None: @pytest.mark.parametrize("fill_value", [dtypes.NA, 2, 2.0, {"a": 2, "b": 1}]) def test_concat_fill_value(self, fill_value) -> None: datasets = [ - Dataset({"a": ("x", [2, 3]), "b": ("x", [-2, 1]), "x": [1, 2]}), - Dataset({"a": ("x", [1, 2]), "b": ("x", [3, -1]), "x": [0, 1]}), + Dataset({"a": ("x", [2, 3]), "b": ("x", [-2, 1])}, {"x": [1, 2]}), + Dataset({"a": ("x", [1, 2]), "b": ("x", [3, -1])}, {"x": [0, 1]}), ] if fill_value == dtypes.NA: # if we supply the default, we expect the missing value for a @@ -987,7 +1034,9 @@ def test_concat_fill_value(self, fill_value) -> None: }, {"x": [0, 1, 2]}, ) - actual = concat(datasets, dim="t", fill_value=fill_value) + actual = concat( + datasets, dim="t", fill_value=fill_value, data_vars="all", join="outer" + ) assert_identical(actual, expected) @pytest.mark.parametrize("dtype", [str, bytes]) @@ -1009,7 +1058,7 @@ def test_concat_str_dtype(self, dtype, dim) -> None: "x2": np.array(["c", "d"], dtype=dtype), } ) - actual = concat([da1, da2], dim=dim) + actual = concat([da1, da2], dim=dim, join="outer") assert np.issubdtype(actual.x2.dtype, dtype) @@ -1034,7 +1083,7 @@ def test_concat_avoids_index_auto_creation(self) -> None: assert combined.indexes == {} # should not raise on stack - combined = concat(datasets, dim="z") + combined = concat(datasets, dim="z", data_vars="all") assert combined["a"].shape == (2, 3, 3) assert combined["a"].dims == ("z", "x", "y") @@ -1099,11 +1148,15 @@ def test_concat(self) -> None: stacked = concat(grouped, pd.Index(ds["x"], name="x")) assert_identical(foo, stacked) - actual2 = concat([foo[0], foo[1]], pd.Index([0, 1])).reset_coords(drop=True) + actual2 = concat( + [foo.isel(x=0), foo.isel(x=1)], pd.Index([0, 1]), coords="all" + ).reset_coords(drop=True) expected = foo[:2].rename({"x": "concat_dim"}) assert_identical(expected, actual2) - actual3 = concat([foo[0], foo[1]], [0, 1]).reset_coords(drop=True) + actual3 = concat( + [foo.isel(x=0), foo.isel(x=1)], [0, 1], coords="all" + ).reset_coords(drop=True) expected = foo[:2].rename({"x": "concat_dim"}) assert_identical(expected, actual3) @@ -1111,7 +1164,7 @@ def test_concat(self) -> None: concat([foo, bar], dim="w", compat="identical") with pytest.raises(ValueError, match=r"not a valid argument"): - concat([foo, bar], dim="w", data_vars="minimal") + concat([foo, bar], dim="w", data_vars="different") def test_concat_encoding(self) -> None: # Regression test for GH1297 @@ -1185,7 +1238,7 @@ def test_concat_fill_value(self, fill_value) -> None: dims=["y", "x"], coords={"x": [1, 2, 3]}, ) - actual = concat((foo, bar), dim="y", fill_value=fill_value) + actual = concat((foo, bar), dim="y", fill_value=fill_value, join="outer") assert_identical(actual, expected) def test_concat_join_kwarg(self) -> None: @@ -1263,7 +1316,7 @@ def test_concat_str_dtype(self, dtype, dim) -> None: dims=["x1", "x2"], coords={"x1": np.array([1, 2]), "x2": np.array(["c", "d"], dtype=dtype)}, ) - actual = concat([da1, da2], dim=dim) + actual = concat([da1, da2], dim=dim, join="outer") assert np.issubdtype(actual.x2.dtype, dtype) @@ -1288,16 +1341,17 @@ def test_concat_attrs_first_variable(attr1, attr2) -> None: assert concat_attrs == attr1 -def test_concat_merge_single_non_dim_coord(): - # TODO: annotating this func fails +def test_concat_merge_single_non_dim_coord() -> None: da1 = DataArray([1, 2, 3], dims="x", coords={"x": [1, 2, 3], "y": 1}) da2 = DataArray([4, 5, 6], dims="x", coords={"x": [4, 5, 6]}) expected = DataArray(range(1, 7), dims="x", coords={"x": range(1, 7), "y": 1}) - for coords in ["different", "minimal"]: - actual = concat([da1, da2], "x", coords=coords) - assert_identical(actual, expected) + actual = concat([da1, da2], "x", coords="minimal", compat="override") + assert_identical(actual, expected) + + actual = concat([da1, da2], "x", coords="different", compat="equals") + assert_identical(actual, expected) with pytest.raises(ValueError, match=r"'y' not present in all datasets."): concat([da1, da2], dim="x", coords="all") @@ -1305,9 +1359,12 @@ def test_concat_merge_single_non_dim_coord(): da1 = DataArray([1, 2, 3], dims="x", coords={"x": [1, 2, 3], "y": 1}) da2 = DataArray([4, 5, 6], dims="x", coords={"x": [4, 5, 6]}) da3 = DataArray([7, 8, 9], dims="x", coords={"x": [7, 8, 9], "y": 1}) - for coords in ["different", "all"]: - with pytest.raises(ValueError, match=r"'y' not present in all datasets"): - concat([da1, da2, da3], dim="x", coords=coords) + + with pytest.raises(ValueError, match=r"'y' not present in all datasets"): + concat([da1, da2, da3], dim="x", coords="all") + + with pytest.raises(ValueError, match=r"'y' not present in all datasets"): + concat([da1, da2, da3], dim="x", coords="different", compat="equals") def test_concat_preserve_coordinate_order() -> None: @@ -1384,6 +1441,106 @@ def test_concat_index_not_same_dim() -> None: concat([ds1, ds2], dim="x") +class TestNewDefaults: + def test_concat_second_empty_with_scalar_data_var_only_on_first(self) -> None: + ds1 = Dataset(data_vars={"a": ("y", [0.1]), "b": 0.1}, coords={"x": 0.1}) + ds2 = Dataset(coords={"x": 0.1}) + + expected = Dataset( + data_vars={"a": ("y", [0.1, np.nan]), "b": 0.1}, coords={"x": 0.1} + ) + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, + match="will change from compat='equals' to compat='override'", + ): + actual = concat( + [ds1, ds2], dim="y", coords="different", data_vars="different" + ) + assert_identical(actual, expected) + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises(ValueError, match="might be related to new default"): + concat([ds1, ds2], dim="y", coords="different", data_vars="different") + + def test_concat_multiple_datasets_missing_vars(self) -> None: + vars_to_drop = [ + "temperature", + "pressure", + "humidity", + "precipitation", + "cloud_cover", + ] + + datasets = create_concat_datasets( + len(vars_to_drop), seed=123, include_day=False + ) + # set up the test data + datasets = [ + ds.drop_vars(varname) + for ds, varname in zip(datasets, vars_to_drop, strict=True) + ] + with set_options(use_new_combine_kwarg_defaults=False): + old = concat(datasets, dim="day") + with set_options(use_new_combine_kwarg_defaults=True): + new = concat(datasets, dim="day") + assert_identical(old, new) + + @pytest.mark.parametrize("coords", ["different", "minimal", "all"]) + def test_concat_coords_kwarg( + self, coords: Literal["all", "minimal", "different"] + ) -> None: + data = create_test_data().drop_dims("dim3") + + # make sure the coords argument behaves as expected + data.coords["extra"] = ("dim4", np.arange(3)) + datasets = [g for _, g in data.groupby("dim1")] + + with set_options(use_new_combine_kwarg_defaults=False): + expectation: AbstractContextManager = ( + pytest.warns( + FutureWarning, + match="will change from compat='equals' to compat='override'", + ) + if coords == "different" + else nullcontext() + ) + with expectation: + old = concat(datasets, data["dim1"], coords=coords) + + with set_options(use_new_combine_kwarg_defaults=True): + if coords == "different": + with pytest.raises(ValueError): + concat(datasets, data["dim1"], coords=coords) + else: + new = concat(datasets, data["dim1"], coords=coords) + assert_identical(old, new) + + def test_concat_promote_shape_for_scalars_with_mixed_lengths_along_concat_dim( + self, + ) -> None: + # values should repeat + objs = [Dataset({"x": [0]}, {"y": -1}), Dataset({"x": [1, 2]}, {"y": -2})] + expected = Dataset({"x": [0, 1, 2]}, {"y": ("x", [-1, -2, -2])}) + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, + match="will change from coords='different' to coords='minimal'", + ): + old = concat(objs, "x") + assert_identical(old, expected) + with set_options(use_new_combine_kwarg_defaults=True): + new = concat(objs, "x") + with pytest.raises(AssertionError): + assert_identical(new, old) + with pytest.raises(ValueError, match="might be related to new default"): + concat(objs, "x", coords="different") + with pytest.raises(merge.MergeError, match="conflicting values"): + concat(objs, "x", compat="equals") + + new = concat(objs, "x", coords="different", compat="equals") + assert_identical(old, new) + + def test_concat_multi_dim_index() -> None: ds1 = ( Dataset( diff --git a/xarray/tests/test_dask.py b/xarray/tests/test_dask.py index 9024f2ae677..22856e6a638 100644 --- a/xarray/tests/test_dask.py +++ b/xarray/tests/test_dask.py @@ -442,7 +442,11 @@ def test_concat_loads_variables(self): assert kernel_call_count == 0 out = xr.concat( - [ds1, ds2, ds3], dim="n", data_vars="different", coords="different" + [ds1, ds2, ds3], + dim="n", + data_vars="different", + coords="different", + compat="equals", ) # each kernel is computed exactly once assert kernel_call_count == 6 @@ -484,7 +488,11 @@ def test_concat_loads_variables(self): # stop computing variables as it would not have any benefit ds4 = Dataset(data_vars={"d": ("x", [2.0])}, coords={"c": ("x", [2.0])}) out = xr.concat( - [ds1, ds2, ds4, ds3], dim="n", data_vars="different", coords="different" + [ds1, ds2, ds4, ds3], + dim="n", + data_vars="different", + coords="different", + compat="equals", ) # the variables of ds1 and ds2 were computed, but those of ds3 didn't assert kernel_call_count == 22 @@ -505,7 +513,11 @@ def test_concat_loads_variables(self): # now check that concat() is correctly using dask name equality to skip loads out = xr.concat( - [ds1, ds1, ds1], dim="n", data_vars="different", coords="different" + [ds1, ds1, ds1], + dim="n", + data_vars="different", + coords="different", + compat="equals", ) assert kernel_call_count == 24 # variables are not loaded in the output @@ -1366,7 +1378,7 @@ def test_map_blocks_ds_transformations(func, map_ds): def test_map_blocks_da_ds_with_template(obj): func = lambda x: x.isel(x=[1]) # a simple .isel(x=[1, 5, 9]) puts all those in a single chunk. - template = xr.concat([obj.isel(x=[i]) for i in [1, 5, 9]], dim="x") + template = xr.concat([obj.isel(x=[i]) for i in [1, 5, 9]], data_vars=None, dim="x") with raise_if_dask_computes(): actual = xr.map_blocks(func, obj, template=template) assert_identical(actual, template) @@ -1439,7 +1451,9 @@ def test_map_blocks_errors_bad_template(obj): xr.map_blocks( lambda a: a.isel(x=[1]).assign_coords(x=[120]), # assign bad index values obj, - template=xr.concat([obj.isel(x=[i]) for i in [1, 5, 9]], dim="x"), + template=xr.concat( + [obj.isel(x=[i]) for i in [1, 5, 9]], data_vars=None, dim="x" + ), ).compute() diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 45c169bb931..71c11626e1e 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1439,12 +1439,26 @@ def test_selection_multiindex_from_level(self) -> None: # GH: 3512 da = DataArray([0, 1], dims=["x"], coords={"x": [0, 1], "y": "a"}) db = DataArray([2, 3], dims=["x"], coords={"x": [0, 1], "y": "b"}) - data = xr.concat([da, db], dim="x").set_index(xy=["x", "y"]) + data = xr.concat( + [da, db], dim="x", coords="different", compat="equals" + ).set_index(xy=["x", "y"]) assert data.dims == ("xy",) actual = data.sel(y="a") expected = data.isel(xy=[0, 1]).unstack("xy").squeeze("y") assert_equal(actual, expected) + def test_concat_with_default_coords_warns(self) -> None: + da = DataArray([0, 1], dims=["x"], coords={"x": [0, 1], "y": "a"}) + db = DataArray([2, 3], dims=["x"], coords={"x": [0, 1], "y": "b"}) + + with pytest.warns(FutureWarning): + original = xr.concat([da, db], dim="x") + assert original.y.size == 4 + with set_options(use_new_combine_kwarg_defaults=True): + # default compat="override" will pick the first one + new = xr.concat([da, db], dim="x") + assert new.y.size == 1 + def test_virtual_default_coords(self) -> None: array = DataArray(np.zeros((5,)), dims="x") expected = DataArray(range(5), dims="x", name="x") diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 20c5d653187..8e4b09fbfeb 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -6240,7 +6240,7 @@ def test_dataset_math_auto_align(self) -> None: assert_equal(actual, expected) actual = ds + ds[["bar"]] - expected = (2 * ds[["bar"]]).merge(ds.coords) + expected = (2 * ds[["bar"]]).merge(ds.coords, compat="override") assert_identical(expected, actual) assert_identical(ds + Dataset(), ds.coords.to_dataset()) @@ -6676,12 +6676,12 @@ def test_combine_first(self) -> None: coords={"x": ["a", "b", "c"]}, ) assert_equal(actual, expected) - assert_equal(actual, xr.merge([dsx0, dsx1])) + assert_equal(actual, xr.merge([dsx0, dsx1], join="outer")) # works just like xr.merge([self, other]) dsy2 = DataArray([2, 2, 2], [("x", ["b", "c", "d"])]).to_dataset(name="dsy2") actual = dsx0.combine_first(dsy2) - expected = xr.merge([dsy2, dsx0]) + expected = xr.merge([dsy2, dsx0], join="outer") assert_equal(actual, expected) def test_sortby(self) -> None: diff --git a/xarray/tests/test_duck_array_wrapping.py b/xarray/tests/test_duck_array_wrapping.py index 42440385928..340669b795f 100644 --- a/xarray/tests/test_duck_array_wrapping.py +++ b/xarray/tests/test_duck_array_wrapping.py @@ -163,7 +163,7 @@ def test_concat(self): assert isinstance(result.data, self.Array) def test_merge(self): - result = xr.merge([self.x1, self.x2], compat="override") + result = xr.merge([self.x1, self.x2], compat="override", join="outer") assert isinstance(result.foo.data, self.Array) def test_where(self): diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 731574baac9..66dfd2c9396 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -2438,6 +2438,7 @@ def test_resample_min_count(self) -> None: for i in range(3) ], dim=actual["time"], + data_vars="all", ) assert_allclose(expected, actual) diff --git a/xarray/tests/test_merge.py b/xarray/tests/test_merge.py index 1b4e1e3e94d..b6c8338494c 100644 --- a/xarray/tests/test_merge.py +++ b/xarray/tests/test_merge.py @@ -5,6 +5,7 @@ import xarray as xr from xarray.core import dtypes +from xarray.core.options import set_options from xarray.structure import merge from xarray.structure.merge import MergeError from xarray.testing import assert_equal, assert_identical @@ -37,15 +38,17 @@ def test_merge_arrays(self): expected = data[["var1", "var2"]] assert_identical(actual, expected) - def test_merge_datasets(self): - data = create_test_data(add_attrs=False, use_extension_array=True) + @pytest.mark.parametrize("use_new_combine_kwarg_defaults", [True, False]) + def test_merge_datasets(self, use_new_combine_kwarg_defaults): + with set_options(use_new_combine_kwarg_defaults=use_new_combine_kwarg_defaults): + data = create_test_data(add_attrs=False, use_extension_array=True) - actual = xr.merge([data[["var1"]], data[["var2"]]]) - expected = data[["var1", "var2"]] - assert_identical(actual, expected) + actual = xr.merge([data[["var1"]], data[["var2"]]]) + expected = data[["var1", "var2"]] + assert_identical(actual, expected) - actual = xr.merge([data, data]) - assert_identical(actual, data) + actual = xr.merge([data, data]) + assert_identical(actual, data) def test_merge_dataarray_unnamed(self): data = xr.DataArray([1, 2], dims="x") @@ -194,9 +197,15 @@ def test_merge_arrays_attrs_variables( if expect_exception: with pytest.raises(MergeError, match="combine_attrs"): - actual = xr.merge([data1, data2], combine_attrs=combine_attrs) + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + actual = xr.merge([data1, data2], combine_attrs=combine_attrs) else: - actual = xr.merge([data1, data2], combine_attrs=combine_attrs) + actual = xr.merge( + [data1, data2], compat="no_conflicts", combine_attrs=combine_attrs + ) expected = xr.Dataset( {"var1": ("dim1", [], expected_attrs)}, coords={"dim1": ("dim1", [], expected_attrs)}, @@ -275,8 +284,12 @@ def test_merge_no_conflicts_single_var(self): ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]}) ds2 = xr.Dataset({"a": ("x", [2, 3]), "x": [1, 2]}) expected = xr.Dataset({"a": ("x", [1, 2, 3]), "x": [0, 1, 2]}) - assert expected.identical(xr.merge([ds1, ds2], compat="no_conflicts")) - assert expected.identical(xr.merge([ds2, ds1], compat="no_conflicts")) + assert expected.identical( + xr.merge([ds1, ds2], compat="no_conflicts", join="outer") + ) + assert expected.identical( + xr.merge([ds2, ds1], compat="no_conflicts", join="outer") + ) assert ds1.identical(xr.merge([ds1, ds2], compat="no_conflicts", join="left")) assert ds2.identical(xr.merge([ds1, ds2], compat="no_conflicts", join="right")) expected = xr.Dataset({"a": ("x", [2]), "x": [1]}) @@ -286,11 +299,11 @@ def test_merge_no_conflicts_single_var(self): with pytest.raises(xr.MergeError): ds3 = xr.Dataset({"a": ("x", [99, 3]), "x": [1, 2]}) - xr.merge([ds1, ds3], compat="no_conflicts") + xr.merge([ds1, ds3], compat="no_conflicts", join="outer") with pytest.raises(xr.MergeError): ds3 = xr.Dataset({"a": ("y", [2, 3]), "y": [1, 2]}) - xr.merge([ds1, ds3], compat="no_conflicts") + xr.merge([ds1, ds3], compat="no_conflicts", join="outer") def test_merge_no_conflicts_multi_var(self): data = create_test_data(add_attrs=False) @@ -317,12 +330,12 @@ def test_merge_no_conflicts_preserve_attrs(self): def test_merge_no_conflicts_broadcast(self): datasets = [xr.Dataset({"x": ("y", [0])}), xr.Dataset({"x": np.nan})] - actual = xr.merge(datasets) + actual = xr.merge(datasets, compat="no_conflicts") expected = xr.Dataset({"x": ("y", [0])}) assert_identical(expected, actual) datasets = [xr.Dataset({"x": ("y", [np.nan])}), xr.Dataset({"x": 0})] - actual = xr.merge(datasets) + actual = xr.merge(datasets, compat="no_conflicts") assert_identical(expected, actual) @@ -345,7 +358,7 @@ def test_merge(self): actual = data.merge(data.reset_coords(drop=True)) assert_identical(data, actual) - with pytest.raises(ValueError): + with pytest.raises(ValueError, match="conflicting values for variable"): ds1.merge(ds2.rename({"var3": "var1"})) with pytest.raises(ValueError, match=r"should be coordinates or not"): data.reset_coords().merge(data) @@ -364,13 +377,13 @@ def test_merge_drop_attrs(self): assert ds1.coords["dim2"].attrs["keep me"] == "example" assert ds2.coords["numbers"].attrs["foo"] == "bar" - def test_merge_broadcast_equals(self): + def test_merge_compat_broadcast_equals(self): ds1 = xr.Dataset({"x": 0}) ds2 = xr.Dataset({"x": ("y", [0, 0])}) - actual = ds1.merge(ds2) + actual = ds1.merge(ds2, compat="broadcast_equals") assert_identical(ds2, actual) - actual = ds2.merge(ds1) + actual = ds2.merge(ds1, compat="broadcast_equals") assert_identical(ds2, actual) actual = ds1.copy() @@ -379,7 +392,7 @@ def test_merge_broadcast_equals(self): ds1 = xr.Dataset({"x": np.nan}) ds2 = xr.Dataset({"x": ("y", [np.nan, np.nan])}) - actual = ds1.merge(ds2) + actual = ds1.merge(ds2, compat="broadcast_equals") assert_identical(ds2, actual) def test_merge_compat(self): @@ -404,6 +417,7 @@ def test_merge_compat(self): assert ds1.identical(ds1.merge(ds2, compat="override")) def test_merge_compat_minimal(self) -> None: + """Test that we drop the conflicting bar coordinate.""" # https://github.com/pydata/xarray/issues/7405 # https://github.com/pydata/xarray/issues/7588 ds1 = xr.Dataset(coords={"foo": [1, 2, 3], "bar": 4}) @@ -413,14 +427,14 @@ def test_merge_compat_minimal(self) -> None: expected = xr.Dataset(coords={"foo": [1, 2, 3]}) assert_identical(actual, expected) - def test_merge_auto_align(self): + def test_merge_join_outer(self): ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]}) ds2 = xr.Dataset({"b": ("x", [3, 4]), "x": [1, 2]}) expected = xr.Dataset( {"a": ("x", [1, 2, np.nan]), "b": ("x", [np.nan, 3, 4])}, {"x": [0, 1, 2]} ) - assert expected.identical(ds1.merge(ds2)) - assert expected.identical(ds2.merge(ds1)) + assert expected.identical(ds1.merge(ds2, join="outer")) + assert expected.identical(ds2.merge(ds1, join="outer")) expected = expected.isel(x=slice(2)) assert expected.identical(ds1.merge(ds2, join="left")) @@ -448,17 +462,19 @@ def test_merge_fill_value(self, fill_value): {"a": ("x", [1, 2, fill_value_a]), "b": ("x", [fill_value_b, 3, 4])}, {"x": [0, 1, 2]}, ) - assert expected.identical(ds1.merge(ds2, fill_value=fill_value)) - assert expected.identical(ds2.merge(ds1, fill_value=fill_value)) - assert expected.identical(xr.merge([ds1, ds2], fill_value=fill_value)) + assert expected.identical(ds1.merge(ds2, join="outer", fill_value=fill_value)) + assert expected.identical(ds2.merge(ds1, join="outer", fill_value=fill_value)) + assert expected.identical( + xr.merge([ds1, ds2], join="outer", fill_value=fill_value) + ) def test_merge_no_conflicts(self): ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]}) ds2 = xr.Dataset({"a": ("x", [2, 3]), "x": [1, 2]}) expected = xr.Dataset({"a": ("x", [1, 2, 3]), "x": [0, 1, 2]}) - assert expected.identical(ds1.merge(ds2, compat="no_conflicts")) - assert expected.identical(ds2.merge(ds1, compat="no_conflicts")) + assert expected.identical(ds1.merge(ds2, compat="no_conflicts", join="outer")) + assert expected.identical(ds2.merge(ds1, compat="no_conflicts", join="outer")) assert ds1.identical(ds1.merge(ds2, compat="no_conflicts", join="left")) @@ -469,11 +485,11 @@ def test_merge_no_conflicts(self): with pytest.raises(xr.MergeError): ds3 = xr.Dataset({"a": ("x", [99, 3]), "x": [1, 2]}) - ds1.merge(ds3, compat="no_conflicts") + ds1.merge(ds3, compat="no_conflicts", join="outer") with pytest.raises(xr.MergeError): ds3 = xr.Dataset({"a": ("y", [2, 3]), "y": [1, 2]}) - ds1.merge(ds3, compat="no_conflicts") + ds1.merge(ds3, compat="no_conflicts", join="outer") def test_merge_dataarray(self): ds = xr.Dataset({"a": 0}) @@ -511,3 +527,76 @@ def test_merge_combine_attrs( actual = ds1.merge(ds2, combine_attrs=combine_attrs) expected = xr.Dataset(attrs=expected_attrs) assert_identical(actual, expected) + + +class TestNewDefaults: + def test_merge_datasets_false_warning(self): + data = create_test_data(add_attrs=False, use_extension_array=True) + + with set_options(use_new_combine_kwarg_defaults=False): + old = xr.merge([data, data]) + + with set_options(use_new_combine_kwarg_defaults=True): + new = xr.merge([data, data]) + + assert_identical(old, new) + + def test_merge(self): + data = create_test_data() + ds1 = data[["var1"]] + ds2 = data[["var3"]] + expected = data[["var1", "var3"]] + with set_options(use_new_combine_kwarg_defaults=True): + actual = ds1.merge(ds2) + assert_identical(expected, actual) + + actual = ds2.merge(ds1) + assert_identical(expected, actual) + + actual = data.merge(data) + assert_identical(data, actual) + + ds1.merge(ds2.rename({"var3": "var1"})) + + with pytest.raises(ValueError, match=r"should be coordinates or not"): + data.reset_coords().merge(data) + with pytest.raises(ValueError, match=r"should be coordinates or not"): + data.merge(data.reset_coords()) + + def test_merge_broadcast_equals(self): + ds1 = xr.Dataset({"x": 0}) + ds2 = xr.Dataset({"x": ("y", [0, 0])}) + + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, + match="will change from compat='no_conflicts' to compat='override'", + ): + old = ds1.merge(ds2) + + with set_options(use_new_combine_kwarg_defaults=True): + new = ds1.merge(ds2) + + assert_identical(ds2, old) + with pytest.raises(AssertionError): + assert_identical(old, new) + + def test_merge_auto_align(self): + ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]}) + ds2 = xr.Dataset({"b": ("x", [3, 4]), "x": [1, 2]}) + expected = xr.Dataset( + {"a": ("x", [1, 2, np.nan]), "b": ("x", [np.nan, 3, 4])}, {"x": [0, 1, 2]} + ) + with set_options(use_new_combine_kwarg_defaults=False): + with pytest.warns( + FutureWarning, match="will change from join='outer' to join='exact'" + ): + assert expected.identical(ds1.merge(ds2)) + with pytest.warns( + FutureWarning, match="will change from join='outer' to join='exact'" + ): + assert expected.identical(ds2.merge(ds1)) + + with set_options(use_new_combine_kwarg_defaults=True): + with pytest.raises(ValueError, match="might be related to new default"): + expected.identical(ds2.merge(ds1)) diff --git a/xarray/tests/test_units.py b/xarray/tests/test_units.py index 7635dde3a69..c955b3cd338 100644 --- a/xarray/tests/test_units.py +++ b/xarray/tests/test_units.py @@ -731,6 +731,9 @@ def test_broadcast_dataset(dtype): "coords", ), ) +@pytest.mark.filterwarnings( + "ignore:.*the default value for coords will change:FutureWarning" +) def test_combine_by_coords(variant, unit, error, dtype): original_unit = unit_registry.m @@ -808,6 +811,12 @@ def test_combine_by_coords(variant, unit, error, dtype): "coords", ), ) +@pytest.mark.filterwarnings( + "ignore:.*the default value for join will change:FutureWarning" +) +@pytest.mark.filterwarnings( + "ignore:.*the default value for compat will change:FutureWarning" +) def test_combine_nested(variant, unit, error, dtype): original_unit = unit_registry.m @@ -1048,6 +1057,12 @@ def test_concat_dataset(variant, unit, error, dtype): "coords", ), ) +@pytest.mark.filterwarnings( + "ignore:.*the default value for join will change:FutureWarning" +) +@pytest.mark.filterwarnings( + "ignore:.*the default value for compat will change:FutureWarning" +) def test_merge_dataarray(variant, unit, error, dtype): original_unit = unit_registry.m @@ -1152,6 +1167,12 @@ def test_merge_dataarray(variant, unit, error, dtype): "coords", ), ) +@pytest.mark.filterwarnings( + "ignore:.*the default value for join will change:FutureWarning" +) +@pytest.mark.filterwarnings( + "ignore:.*the default value for compat will change:FutureWarning" +) def test_merge_dataset(variant, unit, error, dtype): original_unit = unit_registry.m @@ -5570,6 +5591,12 @@ def test_content_manipulation(self, func, variant, dtype): "coords", ), ) + @pytest.mark.filterwarnings( + "ignore:.*the default value for join will change:FutureWarning" + ) + @pytest.mark.filterwarnings( + "ignore:.*the default value for compat will change:FutureWarning" + ) def test_merge(self, variant, unit, error, dtype): left_variants = { "data": (unit_registry.m, 1, 1), diff --git a/xarray/util/deprecation_helpers.py b/xarray/util/deprecation_helpers.py index 1064082872d..7a8ab6ce960 100644 --- a/xarray/util/deprecation_helpers.py +++ b/xarray/util/deprecation_helpers.py @@ -35,8 +35,9 @@ import warnings from collections.abc import Callable from functools import wraps -from typing import TypeVar +from typing import Any, Self, TypeVar +from xarray.core.options import OPTIONS from xarray.core.utils import emit_user_level_warning T = TypeVar("T", bound=Callable) @@ -145,3 +146,76 @@ def wrapper(*args, **kwargs): # We're quite confident we're just returning `T` from this function, so it's fine to ignore typing # within the function. return wrapper # type: ignore[return-value] + + +class CombineKwargDefault: + """Object that handles deprecation cycle for kwarg default values. + + Similar to ReprObject + """ + + _old: str + _new: str | None + _name: str + + def __init__(self, *, name: str, old: str, new: str | None): + self._name = name + self._old = old + self._new = new + + def __repr__(self) -> str: + return str(self._value) + + def __eq__(self, other: Self | Any) -> bool: + return ( + self._value == other._value + if isinstance(other, type(self)) + else self._value == other + ) + + @property + def _value(self) -> str | None: + return self._new if OPTIONS["use_new_combine_kwarg_defaults"] else self._old + + def __hash__(self) -> int: + return hash(self._value) + + def __dask_tokenize__(self) -> object: + from dask.base import normalize_token + + return normalize_token((type(self), self._value)) + + def warning_message(self, message: str, recommend_set_options: bool = True) -> str: + if recommend_set_options: + recommendation = ( + " To opt in to new defaults and get rid of these warnings now " + "use `set_options(use_new_combine_kwarg_defaults=True) or " + f"set {self._name} explicitly." + ) + else: + recommendation = ( + f" The recommendation is to set {self._name} explicitly for this case." + ) + + return ( + f"In a future version of xarray the default value for {self._name} will " + f"change from {self._name}={self._old!r} to {self._name}={self._new!r}. " + + message + + recommendation + ) + + def error_message(self) -> str: + return ( + f" Error might be related to new default (`{self._name}={self._new!r}`). " + f"Previously the default was `{self._name}={self._old!r}`. " + f"The recommendation is to set {self._name!r} explicitly for this case." + ) + + +_DATA_VARS_DEFAULT = CombineKwargDefault(name="data_vars", old="all", new=None) +_COORDS_DEFAULT = CombineKwargDefault(name="coords", old="different", new="minimal") +_COMPAT_CONCAT_DEFAULT = CombineKwargDefault( + name="compat", old="equals", new="override" +) +_COMPAT_DEFAULT = CombineKwargDefault(name="compat", old="no_conflicts", new="override") +_JOIN_DEFAULT = CombineKwargDefault(name="join", old="outer", new="exact")