Skip to content

Commit

Permalink
Use old kwarg values within map_blocks, concat dataarray
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell committed Feb 25, 2025
1 parent e16834f commit 9c50125
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 29 deletions.
13 changes: 5 additions & 8 deletions xarray/core/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 13 additions & 15 deletions xarray/core/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"
)
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions xarray/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 9 additions & 3 deletions xarray/core/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions xarray/util/deprecation_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 9c50125

Please sign in to comment.