Skip to content

Commit 5461a9f

Browse files
committed
Use sentinel value to change default with warnings
1 parent 5c56acf commit 5461a9f

File tree

10 files changed

+344
-82
lines changed

10 files changed

+344
-82
lines changed

xarray/backends/api.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
from xarray.core.utils import is_remote_uri
5050
from xarray.namedarray.daskmanager import DaskManager
5151
from xarray.namedarray.parallelcompat import guess_chunkmanager
52+
from xarray.util.deprecation_helpers import (
53+
_COMPAT_DEFAULT,
54+
_COORDS_DEFAULT,
55+
_DATA_VARS_DEFAULT,
56+
_JOIN_DEFAULT,
57+
CombineKwargDefault,
58+
)
5259

5360
if TYPE_CHECKING:
5461
try:
@@ -1402,14 +1409,16 @@ def open_mfdataset(
14021409
| Sequence[Index]
14031410
| None
14041411
) = None,
1405-
compat: CompatOptions = "no_conflicts",
1412+
compat: CompatOptions | CombineKwargDefault = _COMPAT_DEFAULT,
14061413
preprocess: Callable[[Dataset], Dataset] | None = None,
14071414
engine: T_Engine | None = None,
1408-
data_vars: Literal["all", "minimal", "different"] | list[str] = "all",
1409-
coords="different",
1415+
data_vars: Literal["all", "minimal", "different"]
1416+
| list[str]
1417+
| CombineKwargDefault = _DATA_VARS_DEFAULT,
1418+
coords=_COORDS_DEFAULT,
14101419
combine: Literal["by_coords", "nested"] = "by_coords",
14111420
parallel: bool = False,
1412-
join: JoinOptions = "outer",
1421+
join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT,
14131422
attrs_file: str | os.PathLike | None = None,
14141423
combine_attrs: CombineAttrsOptions = "override",
14151424
**kwargs,
@@ -1596,9 +1605,6 @@ def open_mfdataset(
15961605

15971606
paths1d: list[str | ReadBuffer]
15981607
if combine == "nested":
1599-
if isinstance(concat_dim, str | DataArray) or concat_dim is None:
1600-
concat_dim = [concat_dim] # type: ignore[assignment]
1601-
16021608
# This creates a flat list which is easier to iterate over, whilst
16031609
# encoding the originally-supplied structure as "ids".
16041610
# The "ids" are not used at all if combine='by_coords`.
@@ -1647,7 +1653,7 @@ def open_mfdataset(
16471653
# along each dimension, using structure given by "ids"
16481654
combined = _nested_combine(
16491655
datasets,
1650-
concat_dims=concat_dim,
1656+
concat_dim=concat_dim,
16511657
compat=compat,
16521658
data_vars=data_vars,
16531659
coords=coords,

xarray/core/alignment.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import functools
44
import operator
5+
import warnings
56
from collections import defaultdict
67
from collections.abc import Callable, Hashable, Iterable, Mapping
78
from contextlib import suppress
@@ -22,6 +23,7 @@
2223
from xarray.core.types import T_Alignable
2324
from xarray.core.utils import is_dict_like, is_full_slice
2425
from xarray.core.variable import Variable, as_compatible_data, calculate_dimensions
26+
from xarray.util.deprecation_helpers import CombineKwargDefault
2527

2628
if TYPE_CHECKING:
2729
from xarray.core.dataarray import DataArray
@@ -418,12 +420,38 @@ def align_indexes(self) -> None:
418420
else:
419421
need_reindex = False
420422
if need_reindex:
423+
if (
424+
isinstance(self.join, CombineKwargDefault)
425+
and self.join != "exact"
426+
):
427+
warnings.warn(
428+
self.join.warning_message(
429+
"This change will result in the following ValueError:"
430+
"cannot be aligned with join='exact' because "
431+
"index/labels/sizes are not equal along "
432+
"these coordinates (dimensions): "
433+
+ ", ".join(
434+
f"{name!r} {dims!r}" for name, dims in key[0]
435+
),
436+
recommend_set_options=False,
437+
),
438+
category=FutureWarning,
439+
stacklevel=2,
440+
)
421441
if self.join == "exact":
442+
new_default_warning = (
443+
" Failure might be related to new default (join='exact'). "
444+
"Previously the default was join='outer'. "
445+
"The recommendation is to set join explicitly for this case."
446+
)
422447
raise ValueError(
423448
"cannot align objects with join='exact' where "
424449
"index/labels/sizes are not equal along "
425450
"these coordinates (dimensions): "
426451
+ ", ".join(f"{name!r} {dims!r}" for name, dims in key[0])
452+
+ new_default_warning
453+
if isinstance(self.join, CombineKwargDefault)
454+
else ""
427455
)
428456
joiner = self._get_index_joiner(index_cls)
429457
joined_index = joiner(matching_indexes)
@@ -886,7 +914,7 @@ def align(
886914

887915
def deep_align(
888916
objects: Iterable[Any],
889-
join: JoinOptions = "inner",
917+
join: JoinOptions | CombineKwargDefault = "inner",
890918
copy: bool = True,
891919
indexes=None,
892920
exclude: str | Iterable[Hashable] = frozenset(),

xarray/core/combine.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
from xarray.core.dataset import Dataset
1313
from xarray.core.merge import merge
1414
from xarray.core.utils import iterate_nested
15+
from xarray.util.deprecation_helpers import (
16+
_COMPAT_DEFAULT,
17+
_COORDS_DEFAULT,
18+
_DATA_VARS_DEFAULT,
19+
_JOIN_DEFAULT,
20+
CombineKwargDefault,
21+
)
1522

1623
if TYPE_CHECKING:
1724
from xarray.core.types import (
@@ -202,9 +209,9 @@ def _combine_nd(
202209
concat_dims,
203210
data_vars,
204211
coords,
205-
compat: CompatOptions,
212+
compat: CompatOptions | CombineKwargDefault,
206213
fill_value,
207-
join: JoinOptions,
214+
join: JoinOptions | CombineKwargDefault,
208215
combine_attrs: CombineAttrsOptions,
209216
):
210217
"""
@@ -264,7 +271,7 @@ def _combine_all_along_first_dim(
264271
coords,
265272
compat: CompatOptions,
266273
fill_value,
267-
join: JoinOptions,
274+
join: JoinOptions | CombineKwargDefault,
268275
combine_attrs: CombineAttrsOptions,
269276
):
270277
# Group into lines of datasets which must be combined along dim
@@ -295,7 +302,7 @@ def _combine_1d(
295302
data_vars,
296303
coords,
297304
fill_value,
298-
join: JoinOptions,
305+
join: JoinOptions | CombineKwargDefault,
299306
combine_attrs: CombineAttrsOptions,
300307
):
301308
"""
@@ -345,18 +352,21 @@ def _new_tile_id(single_id_ds_pair):
345352

346353
def _nested_combine(
347354
datasets,
348-
concat_dims,
355+
concat_dim,
349356
compat,
350357
data_vars,
351358
coords,
352359
ids,
353360
fill_value,
354-
join: JoinOptions,
361+
join: JoinOptions | CombineKwargDefault,
355362
combine_attrs: CombineAttrsOptions,
356363
):
357364
if len(datasets) == 0:
358365
return Dataset()
359366

367+
if isinstance(concat_dim, str | DataArray) or concat_dim is None:
368+
concat_dim = [concat_dim] # type: ignore[assignment]
369+
360370
# Arrange datasets for concatenation
361371
# Use information from the shape of the user input
362372
if not ids:
@@ -373,7 +383,7 @@ def _nested_combine(
373383
# Apply series of concatenate or merge operations along each dimension
374384
combined = _combine_nd(
375385
combined_ids,
376-
concat_dims,
386+
concat_dims=concat_dim,
377387
compat=compat,
378388
data_vars=data_vars,
379389
coords=coords,
@@ -391,11 +401,11 @@ def _nested_combine(
391401
def combine_nested(
392402
datasets: DATASET_HYPERCUBE,
393403
concat_dim: str | DataArray | None | Sequence[str | DataArray | pd.Index | None],
394-
compat: str = "no_conflicts",
395-
data_vars: str = "all",
396-
coords: str = "different",
404+
compat: str | CombineKwargDefault = _COMPAT_DEFAULT,
405+
data_vars: str | CombineKwargDefault = _DATA_VARS_DEFAULT,
406+
coords: str | CombineKwargDefault = _COORDS_DEFAULT,
397407
fill_value: object = dtypes.NA,
398-
join: JoinOptions = "outer",
408+
join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT,
399409
combine_attrs: CombineAttrsOptions = "drop",
400410
) -> Dataset:
401411
"""
@@ -588,13 +598,10 @@ def combine_nested(
588598
if mixed_datasets_and_arrays:
589599
raise ValueError("Can't combine datasets with unnamed arrays.")
590600

591-
if isinstance(concat_dim, str | DataArray) or concat_dim is None:
592-
concat_dim = [concat_dim]
593-
594601
# The IDs argument tells _nested_combine that datasets aren't yet sorted
595602
return _nested_combine(
596603
datasets,
597-
concat_dims=concat_dim,
604+
concat_dim=concat_dim,
598605
compat=compat,
599606
data_vars=data_vars,
600607
coords=coords,
@@ -629,8 +636,8 @@ def _combine_single_variable_hypercube(
629636
fill_value,
630637
data_vars,
631638
coords,
632-
compat: CompatOptions,
633-
join: JoinOptions,
639+
compat: CompatOptions | CombineKwargDefault,
640+
join: JoinOptions | CombineKwargDefault,
634641
combine_attrs: CombineAttrsOptions,
635642
):
636643
"""
@@ -685,11 +692,13 @@ def _combine_single_variable_hypercube(
685692

686693
def combine_by_coords(
687694
data_objects: Iterable[Dataset | DataArray] = [],
688-
compat: CompatOptions = "no_conflicts",
689-
data_vars: Literal["all", "minimal", "different"] | list[str] = "all",
690-
coords: str = "different",
695+
compat: CompatOptions | CombineKwargDefault = _COMPAT_DEFAULT,
696+
data_vars: Literal["all", "minimal", "different"]
697+
| list[str]
698+
| CombineKwargDefault = _DATA_VARS_DEFAULT,
699+
coords: str | CombineKwargDefault = _COORDS_DEFAULT,
691700
fill_value: object = dtypes.NA,
692-
join: JoinOptions = "outer",
701+
join: JoinOptions | CombineKwargDefault = _JOIN_DEFAULT,
693702
combine_attrs: CombineAttrsOptions = "no_conflicts",
694703
) -> Dataset | DataArray:
695704
"""

0 commit comments

Comments
 (0)