Skip to content

Commit 114845f

Browse files
committed
Add data_vars=None as default.
1 parent bec25f6 commit 114845f

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

xarray/structure/combine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,15 @@ def combine_nested(
463463
data_vars : {"minimal", "different", "all" or list of str}, optional
464464
These data variables will be concatenated together:
465465
* "minimal": Only data variables in which the dimension already
466-
appears are included. If concatenating over a dimension _not_
467-
present in any of the objects, then all data variables will
468-
be concatenated along that new dimension.
466+
appears are included.
469467
* "different": Data variables which are not equal (ignoring
470468
attributes) across all datasets are also concatenated (as well as
471469
all for which dimension already appears). Beware: this option may
472470
load the data payload of data variables into memory if they are not
473471
already loaded.
474472
* "all": All data variables will be concatenated.
473+
* None: Means ``"all"`` if ``dim`` is not present in any of the ``objs``,
474+
and ``"minimal"`` if ``dim`` is present in any of ``objs``.
475475
* list of dims: The listed data variables will be concatenated, in
476476
addition to the "minimal" data variables.
477477

xarray/structure/concat.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
JoinOptions,
3838
)
3939

40-
T_DataVars = Union[ConcatOptions, Iterable[Hashable]]
40+
T_DataVars = Union[ConcatOptions, Iterable[Hashable], None]
4141

4242

4343
# TODO: replace dim: Any by 1D array_likes
@@ -98,18 +98,18 @@ def concat(
9898
unchanged. If dimension is provided as a Variable, DataArray or Index, its name
9999
is used as the dimension to concatenate along and the values are added
100100
as a coordinate.
101-
data_vars : {"minimal", "different", "all"} or list of Hashable, optional
101+
data_vars : {"minimal", "different", "all", None} or list of Hashable, optional
102102
These data variables will be concatenated together:
103103
* "minimal": Only data variables in which the dimension already
104-
appears are included. If concatenating over a dimension _not_
105-
present in any of the objects, then all data variables will
106-
be concatenated along that new dimension.
104+
appears are included.
107105
* "different": Data variables which are not equal (ignoring
108106
attributes) across all datasets are also concatenated (as well as
109107
all for which dimension already appears). Beware: this option may
110108
load the data payload of data variables into memory if they are not
111109
already loaded.
112110
* "all": All data variables will be concatenated.
111+
* None: Means ``"all"`` if ``dim`` is not present in any of the ``objs``,
112+
and ``"minimal"`` if ``dim`` is present in any of ``objs``.
113113
* list of dims: The listed data variables will be concatenated, in
114114
addition to the "minimal" data variables.
115115
@@ -352,7 +352,7 @@ def _calc_concat_over(
352352
# variables to be concatenated
353353
concat_over = set()
354354
# variables checked for equality
355-
equals: dict[Hashable, bool] = {}
355+
equals: dict[Hashable, bool | None] = {}
356356
# skip merging these variables.
357357
# if concatenating over a dimension 'x' that is associated with an index over 2 variables,
358358
# 'x' and 'y', then we assert join="equals" on `y` and don't need to merge it.
@@ -365,6 +365,11 @@ def _calc_concat_over(
365365
else:
366366
concat_over_existing_dim = False
367367

368+
if data_vars is None or (
369+
isinstance(data_vars, CombineKwargDefault) and data_vars._value is None
370+
):
371+
data_vars = "minimal" if concat_over_existing_dim else "all"
372+
368373
concat_dim_lengths = []
369374
for ds in datasets:
370375
if concat_over_existing_dim and dim not in ds.dims and dim in ds:
@@ -380,6 +385,7 @@ def process_subset_opt(opt, subset: Literal["coords", "data_vars"]) -> None:
380385
compat_str = (
381386
compat._value if isinstance(compat, CombineKwargDefault) else compat
382387
)
388+
assert compat_str is not None
383389
if isinstance(opt, str | CombineKwargDefault):
384390
if opt == "different":
385391
if isinstance(compat, CombineKwargDefault) and compat != "override":
@@ -467,20 +473,13 @@ def process_subset_opt(opt, subset: Literal["coords", "data_vars"]) -> None:
467473
)
468474
)
469475
elif opt == "minimal":
470-
# with "minimal" concatenation over a new dimension;
471-
# we act like "all" for data_vars
472-
if not concat_over_existing_dim and subset == "data_vars":
473-
concat_over.update(
474-
set().union(
475-
*[set(getattr(d, subset)) - set(d.dims) for d in datasets]
476-
)
477-
)
476+
pass
478477
else:
479478
raise ValueError(f"unexpected value for {subset}: {opt}")
480479

481480
if (
482481
isinstance(opt, CombineKwargDefault)
483-
and opt != "minimal"
482+
and opt._value is not None
484483
and original != concat_over
485484
and concat_over_existing_dim
486485
):

xarray/structure/merge.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def unique_variable(
148148
compat_str = (
149149
compat._value if isinstance(compat, CombineKwargDefault) else compat
150150
)
151+
assert compat_str is not None
151152
# first check without comparing values i.e. no computes
152153
for var in variables[1:]:
153154
equals = getattr(out, compat_str)(var, equiv=lazy_array_equiv)

xarray/tests/test_concat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ def test_concat_constant_index(self, data_vars) -> None:
628628
def test_concat_constant_index_minimal(self) -> None:
629629
ds1 = Dataset({"foo": 1.5}, {"y": 1})
630630
ds2 = Dataset({"foo": 2.5}, {"y": 1})
631-
actual = concat([ds1, ds2], "new_dim", data_vars="minimal", compat="equals")
631+
actual = concat([ds1, ds2], "new_dim", data_vars=None, compat="equals")
632632
expected = Dataset(
633633
{"foo": ("new_dim", [1.5, 2.5])},
634634
coords={"y": 1},

xarray/util/deprecation_helpers.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import warnings
3636
from collections.abc import Callable
3737
from functools import wraps
38-
from typing import Any, TypeVar
38+
from typing import Any, Self, TypeVar
3939

4040
from xarray.core.options import OPTIONS
41-
from xarray.core.utils import ReprObject, emit_user_level_warning
41+
from xarray.core.utils import emit_user_level_warning
4242

4343
T = TypeVar("T", bound=Callable)
4444

@@ -155,26 +155,26 @@ class CombineKwargDefault:
155155
"""
156156

157157
_old: str
158-
_new: str
158+
_new: str | None
159159
_name: str
160160

161-
def __init__(self, *, name: str, old: str, new: str):
161+
def __init__(self, *, name: str, old: str, new: str | None):
162162
self._name = name
163163
self._old = old
164164
self._new = new
165165

166-
def __repr__(self) -> str:
166+
def __repr__(self) -> str | None:
167167
return self._value
168168

169-
def __eq__(self, other: ReprObject | Any) -> bool:
169+
def __eq__(self, other: Self | Any) -> bool:
170170
return (
171171
self._value == other._value
172-
if isinstance(other, ReprObject)
172+
if isinstance(other, type(self))
173173
else self._value == other
174174
)
175175

176176
@property
177-
def _value(self) -> str:
177+
def _value(self) -> str | None:
178178
return self._new if OPTIONS["use_new_combine_kwarg_defaults"] else self._old
179179

180180
def __hash__(self) -> int:
@@ -212,7 +212,7 @@ def error_message(self) -> str:
212212
)
213213

214214

215-
_DATA_VARS_DEFAULT = CombineKwargDefault(name="data_vars", old="all", new="minimal")
215+
_DATA_VARS_DEFAULT = CombineKwargDefault(name="data_vars", old="all", new=None)
216216
_COORDS_DEFAULT = CombineKwargDefault(name="coords", old="different", new="minimal")
217217
_COMPAT_CONCAT_DEFAULT = CombineKwargDefault(
218218
name="compat", old="equals", new="override"

0 commit comments

Comments
 (0)