From 9c50125ab69d2fc4356e377fc8a3c89a66834e3c Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Tue, 25 Feb 2025 15:18:21 -0500 Subject: [PATCH] Use old kwarg values within map_blocks, concat dataarray --- xarray/core/alignment.py | 13 +++++-------- xarray/core/concat.py | 28 +++++++++++++--------------- xarray/core/merge.py | 2 +- xarray/core/options.py | 11 +++++++++-- xarray/core/parallel.py | 12 +++++++++--- xarray/util/deprecation_helpers.py | 7 +++++++ 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/xarray/core/alignment.py b/xarray/core/alignment.py index b2114155cbd..dd7edbd88c2 100644 --- a/xarray/core/alignment.py +++ b/xarray/core/alignment.py @@ -439,19 +439,16 @@ def align_indexes(self) -> None: stacklevel=2, ) if self.join == "exact": - new_default_warning = ( - " Failure might be related to new default (join='exact'). " - "Previously the default was join='outer'. " - "The recommendation is to set join explicitly for this case." - ) raise ValueError( "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]) - + new_default_warning - if isinstance(self.join, CombineKwargDefault) - else "" + + ( + self.join.error_message() + if isinstance(self.join, CombineKwargDefault) + else "" + ) ) joiner = self._get_index_joiner(index_cls) joined_index = joiner(matching_indexes) diff --git a/xarray/core/concat.py b/xarray/core/concat.py index c8230651aa6..846f52bae17 100644 --- a/xarray/core/concat.py +++ b/xarray/core/concat.py @@ -368,12 +368,10 @@ def process_subset_opt(opt, subset): if isinstance(opt, str | CombineKwargDefault): if opt == "different": if isinstance(compat, CombineKwargDefault) and compat != "override": - if subset == "data_vars" or not isinstance( - opt, CombineKwargDefault - ): + if not isinstance(opt, CombineKwargDefault): warnings.warn( compat.warning_message( - "This change will result in the following ValueError:" + "This change will result in the following ValueError: " f"Cannot specify both {subset}='different' and compat='override'.", recommend_set_options=False, ), @@ -382,16 +380,13 @@ def process_subset_opt(opt, subset): ) if compat == "override": - new_default_warning = ( - " Failure might be related to new default (compat='override'). " - "Previously the default was compat='equals' or compat='no_conflicts'. " - "The recommendation is to set compat explicitly for this case." - ) raise ValueError( f"Cannot specify both {subset}='different' and compat='override'." - + new_default_warning - if isinstance(compat, CombineKwargDefault) - else "" + + ( + 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): @@ -469,7 +464,7 @@ def process_subset_opt(opt, subset): ): warnings.warn( opt.warning_message( - "This is likely to lead to different results when multiple datasets" + "This is likely to lead to different results when multiple datasets " "have matching variables with overlapping values.", ), category=FutureWarning, @@ -800,7 +795,10 @@ def _dataarray_concat( "The elements in the input list need to be either all 'Dataset's or all 'DataArray's" ) - if not isinstance(data_vars, CombineKwargDefault) and data_vars != "all": + if not isinstance(data_vars, CombineKwargDefault) and data_vars not in [ + "all", + "minimal", + ]: raise ValueError( "data_vars is not a valid argument when concatenating DataArray objects" ) @@ -819,7 +817,7 @@ def _dataarray_concat( ds = _dataset_concat( datasets, dim=dim, - data_vars=data_vars, + data_vars="all", coords=coords, compat=compat, positions=positions, diff --git a/xarray/core/merge.py b/xarray/core/merge.py index 4a4100cde13..8c14582982b 100644 --- a/xarray/core/merge.py +++ b/xarray/core/merge.py @@ -308,7 +308,7 @@ def merge_collected( ): warnings.warn( compat.warning_message( - "This is likely to lead to different results when" + "This is likely to lead to different results when " "combining overlapping variables with the same name.", ), category=FutureWarning, diff --git a/xarray/core/options.py b/xarray/core/options.py index f17cd8ab9d0..f5eb72f37b4 100644 --- a/xarray/core/options.py +++ b/xarray/core/options.py @@ -86,7 +86,7 @@ class T_Options(TypedDict): "warn_for_unclosed_files": False, "use_bottleneck": True, "use_flox": True, - "use_new_combine_kwarg_defaults": False, + "use_new_combine_kwarg_defaults": True, "use_numbagg": True, "use_opt_einsum": True, } @@ -255,7 +255,14 @@ class set_options: 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 default kwarg values for open_mfdataset. + 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``: "minimal" + * ``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 6d6a6672470..d70a3b8b516 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="outer", + compat="no_conflicts", ).coords # check all dims are present @@ -439,7 +441,9 @@ 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="outer", compat="no_conflicts" + ).coords _, npargs = unzip( sorted( @@ -472,7 +476,9 @@ def _wrapper( ) coordinates = merge( - (preserved_coords, template.coords.to_dataset()[new_coord_vars]) + (preserved_coords, template.coords.to_dataset()[new_coord_vars]), + join="outer", + compat="no_conflicts", ).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/util/deprecation_helpers.py b/xarray/util/deprecation_helpers.py index 44c13560736..ddae2cdf9f3 100644 --- a/xarray/util/deprecation_helpers.py +++ b/xarray/util/deprecation_helpers.py @@ -194,6 +194,13 @@ def warning_message(self, message: str, recommend_set_options: bool = True): + recommendation ) + def error_message(self): + 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} explicitly for this case." + ) + _DATA_VARS_DEFAULT = CombineKwargDefault(name="data_vars", old="all", new="minimal") _COORDS_DEFAULT = CombineKwargDefault(name="coords", old="different", new="minimal")