Skip to content

Commit 0e65034

Browse files
committed
Add option for using new default kwargs
1 parent 5c56acf commit 0e65034

File tree

9 files changed

+315
-80
lines changed

9 files changed

+315
-80
lines changed

xarray/backends/api.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -1402,14 +1402,14 @@ def open_mfdataset(
14021402
| Sequence[Index]
14031403
| None
14041404
) = None,
1405-
compat: CompatOptions = "no_conflicts",
1405+
compat: CompatOptions | None = None,
14061406
preprocess: Callable[[Dataset], Dataset] | None = None,
14071407
engine: T_Engine | None = None,
1408-
data_vars: Literal["all", "minimal", "different"] | list[str] = "all",
1409-
coords="different",
1408+
data_vars: Literal["all", "minimal", "different"] | list[str] | None = None,
1409+
coords=None,
14101410
combine: Literal["by_coords", "nested"] = "by_coords",
14111411
parallel: bool = False,
1412-
join: JoinOptions = "outer",
1412+
join: JoinOptions | None = None,
14131413
attrs_file: str | os.PathLike | None = None,
14141414
combine_attrs: CombineAttrsOptions = "override",
14151415
**kwargs,
@@ -1596,9 +1596,6 @@ def open_mfdataset(
15961596

15971597
paths1d: list[str | ReadBuffer]
15981598
if combine == "nested":
1599-
if isinstance(concat_dim, str | DataArray) or concat_dim is None:
1600-
concat_dim = [concat_dim] # type: ignore[assignment]
1601-
16021599
# This creates a flat list which is easier to iterate over, whilst
16031600
# encoding the originally-supplied structure as "ids".
16041601
# The "ids" are not used at all if combine='by_coords`.
@@ -1647,7 +1644,7 @@ def open_mfdataset(
16471644
# along each dimension, using structure given by "ids"
16481645
combined = _nested_combine(
16491646
datasets,
1650-
concat_dims=concat_dim,
1647+
concat_dim=concat_dim,
16511648
compat=compat,
16521649
data_vars=data_vars,
16531650
coords=coords,

xarray/core/alignment.py

+28-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
indexes_all_equal,
2020
safe_cast_to_index,
2121
)
22+
from xarray.core.options import _new_default_combine_kwargs_warning
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
@@ -113,6 +114,7 @@ class Aligner(Generic[T_Alignable]):
113114
results: tuple[T_Alignable, ...]
114115
objects_matching_indexes: tuple[dict[MatchingIndexKey, Index], ...]
115116
join: str
117+
join_is_default: bool = False
116118
exclude_dims: frozenset[Hashable]
117119
exclude_vars: frozenset[Hashable]
118120
copy: bool
@@ -418,12 +420,31 @@ def align_indexes(self) -> None:
418420
else:
419421
need_reindex = False
420422
if need_reindex:
423+
if self.join_is_default and self.join != "exact":
424+
_new_default_combine_kwargs_warning(
425+
"join",
426+
self.join,
427+
"This change will result in the following ValueError:"
428+
"cannot be aligned with join='exact' because "
429+
"index/labels/sizes are not equal along "
430+
"these coordinates (dimensions): "
431+
+ ", ".join(f"{name!r} {dims!r}" for name, dims in key[0]),
432+
recommend_set_options=False,
433+
)
421434
if self.join == "exact":
435+
new_default_warning = (
436+
"Failure might be related to new default (join='exact'). "
437+
"Previously the default was join='outer'. "
438+
"The recommendation is to set join explicitly for this case."
439+
)
422440
raise ValueError(
423441
"cannot align objects with join='exact' where "
424442
"index/labels/sizes are not equal along "
425443
"these coordinates (dimensions): "
426444
+ ", ".join(f"{name!r} {dims!r}" for name, dims in key[0])
445+
+ new_default_warning
446+
if self.join_is_default
447+
else ""
427448
)
428449
joiner = self._get_index_joiner(index_cls)
429450
joined_index = joiner(matching_indexes)
@@ -892,6 +913,7 @@ def deep_align(
892913
exclude: str | Iterable[Hashable] = frozenset(),
893914
raise_on_invalid: bool = True,
894915
fill_value=dtypes.NA,
916+
join_is_default: bool = False,
895917
) -> list[Any]:
896918
"""Align objects for merging, recursing into dictionary values.
897919
@@ -944,14 +966,17 @@ def is_alignable(obj):
944966
else:
945967
out.append(variables)
946968

947-
aligned = align(
948-
*targets,
969+
aligner = Aligner(
970+
targets,
949971
join=join,
950972
copy=copy,
951973
indexes=indexes,
952-
exclude=exclude,
974+
exclude_dims=exclude,
953975
fill_value=fill_value,
954976
)
977+
aligner.join_is_default = join_is_default
978+
aligner.align()
979+
aligned = list(aligner.results)
955980

956981
for position, key, aligned_obj in zip(positions, keys, aligned, strict=True):
957982
if key is no_key:

xarray/core/combine.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ def _combine_nd(
202202
concat_dims,
203203
data_vars,
204204
coords,
205-
compat: CompatOptions,
205+
compat: CompatOptions | None,
206206
fill_value,
207-
join: JoinOptions,
207+
join: JoinOptions | None,
208208
combine_attrs: CombineAttrsOptions,
209209
):
210210
"""
@@ -314,6 +314,7 @@ def _combine_1d(
314314
fill_value=fill_value,
315315
join=join,
316316
combine_attrs=combine_attrs,
317+
_from_concat=False,
317318
)
318319
except ValueError as err:
319320
if "encountered unexpected variable" in str(err):
@@ -345,18 +346,21 @@ def _new_tile_id(single_id_ds_pair):
345346

346347
def _nested_combine(
347348
datasets,
348-
concat_dims,
349+
concat_dim,
349350
compat,
350351
data_vars,
351352
coords,
352353
ids,
353354
fill_value,
354-
join: JoinOptions,
355+
join,
355356
combine_attrs: CombineAttrsOptions,
356357
):
357358
if len(datasets) == 0:
358359
return Dataset()
359360

361+
if isinstance(concat_dim, str | DataArray) or concat_dim is None:
362+
concat_dim = [concat_dim] # type: ignore[assignment]
363+
360364
# Arrange datasets for concatenation
361365
# Use information from the shape of the user input
362366
if not ids:
@@ -373,10 +377,10 @@ def _nested_combine(
373377
# Apply series of concatenate or merge operations along each dimension
374378
combined = _combine_nd(
375379
combined_ids,
376-
concat_dims,
377-
compat=compat,
380+
concat_dims=concat_dim,
378381
data_vars=data_vars,
379382
coords=coords,
383+
compat=compat,
380384
fill_value=fill_value,
381385
join=join,
382386
combine_attrs=combine_attrs,
@@ -391,11 +395,11 @@ def _nested_combine(
391395
def combine_nested(
392396
datasets: DATASET_HYPERCUBE,
393397
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",
398+
compat: str | None = None,
399+
data_vars: str | None = None,
400+
coords: str | None = None,
397401
fill_value: object = dtypes.NA,
398-
join: JoinOptions = "outer",
402+
join: JoinOptions | None = None,
399403
combine_attrs: CombineAttrsOptions = "drop",
400404
) -> Dataset:
401405
"""
@@ -588,13 +592,10 @@ def combine_nested(
588592
if mixed_datasets_and_arrays:
589593
raise ValueError("Can't combine datasets with unnamed arrays.")
590594

591-
if isinstance(concat_dim, str | DataArray) or concat_dim is None:
592-
concat_dim = [concat_dim]
593-
594595
# The IDs argument tells _nested_combine that datasets aren't yet sorted
595596
return _nested_combine(
596597
datasets,
597-
concat_dims=concat_dim,
598+
concat_dim=concat_dim,
598599
compat=compat,
599600
data_vars=data_vars,
600601
coords=coords,
@@ -629,8 +630,8 @@ def _combine_single_variable_hypercube(
629630
fill_value,
630631
data_vars,
631632
coords,
632-
compat: CompatOptions,
633-
join: JoinOptions,
633+
compat: CompatOptions | None,
634+
join: JoinOptions | None,
634635
combine_attrs: CombineAttrsOptions,
635636
):
636637
"""
@@ -685,11 +686,11 @@ def _combine_single_variable_hypercube(
685686

686687
def combine_by_coords(
687688
data_objects: Iterable[Dataset | DataArray] = [],
688-
compat: CompatOptions = "no_conflicts",
689-
data_vars: Literal["all", "minimal", "different"] | list[str] = "all",
690-
coords: str = "different",
689+
compat: CompatOptions | None = None,
690+
data_vars: Literal["all", "minimal", "different"] | list[str] | None = None,
691+
coords: str | None = None,
691692
fill_value: object = dtypes.NA,
692-
join: JoinOptions = "outer",
693+
join: JoinOptions | None = None,
693694
combine_attrs: CombineAttrsOptions = "no_conflicts",
694695
) -> Dataset | DataArray:
695696
"""

0 commit comments

Comments
 (0)