Skip to content

Commit aa2072e

Browse files
committed
Cleanups
1 parent 518cd6e commit aa2072e

File tree

6 files changed

+52
-22
lines changed

6 files changed

+52
-22
lines changed

doc/whats-new.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,6 @@ New Features
278278
(:issue:`9914`, :pull:`10336`)
279279
By `Joseph Nowak <https://github.com/josephnowak>`_.
280280

281-
Breaking changes
282-
~~~~~~~~~~~~~~~~
283-
284281
Documentation
285282
~~~~~~~~~~~~~
286283
- HTML reprs! By `Scott Henderson <https://github.com/scottyhq>`_.
@@ -8429,8 +8426,6 @@ Backwards incompatible changes
84298426

84308427
Now, the default always concatenates data variables:
84318428

8432-
.. ipython:: python
8433-
:verbatim:
84348429
.. code:: python
84358430
84368431
In [1]: ds = xray.Dataset({"x": 0})

xarray/core/parallel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ def _wrapper(
481481

482482
coordinates = merge(
483483
(preserved_coords, template.coords.to_dataset()[new_coord_vars]),
484+
# FIXME: this should be join="exact", but breaks a test
484485
join="outer",
485486
compat="override",
486487
).coords

xarray/tests/arrays.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from collections.abc import Callable, Iterable
6-
from typing import Any
6+
from typing import Any, Self
77

88
import numpy as np
99

@@ -126,6 +126,23 @@ def broadcast_to(
126126
return ConcatenatableArray(result)
127127

128128

129+
@implements(np.full_like)
130+
def full_like(
131+
x: "ConcatenatableArray", /, fill_value, **kwargs
132+
) -> "ConcatenatableArray":
133+
"""
134+
Broadcasts an array to a specified shape, by either manipulating chunk keys or copying chunk manifest entries.
135+
"""
136+
if not isinstance(x, ConcatenatableArray):
137+
raise TypeError
138+
return ConcatenatableArray(np.full(x.shape, fill_value=fill_value, **kwargs))
139+
140+
141+
@implements(np.all)
142+
def numpy_all(x: "ConcatenatableArray", **kwargs) -> "ConcatenatableArray":
143+
return type(x)(np.all(x._array, **kwargs))
144+
145+
129146
class ConcatenatableArray:
130147
"""Disallows loading or coercing to an index but does support concatenation / stacking."""
131148

@@ -169,6 +186,9 @@ def __getitem__(self, key) -> "ConcatenatableArray":
169186
raise UnexpectedDataAccess("Tried accessing data.")
170187
return ConcatenatableArray(arr)
171188

189+
def __eq__(self, other: "ConcatenatableArray") -> "ConcatenatableArray":
190+
return ConcatenatableArray(self._array == other._array)
191+
172192
def __array_function__(self, func, types, args, kwargs) -> Any:
173193
if func not in CONCATENATABLEARRAY_HANDLED_ARRAY_FUNCTIONS:
174194
return NotImplemented
@@ -190,3 +210,9 @@ def astype(self, dtype: np.dtype, /, *, copy: bool = True) -> "ConcatenatableArr
190210
raise NotImplementedError()
191211
else:
192212
return self
213+
214+
def __and__(self, other: Self) -> "ConcatenatableArray":
215+
return type(self)(self._array & other._array)
216+
217+
def __or__(self, other: Self) -> "ConcatenatableArray":
218+
return type(self)(self._array | other._array)

xarray/tests/test_concat.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,13 @@ def test_concat_multiple_datasets_missing_vars(include_day: bool) -> None:
273273
"cloud_cover",
274274
]
275275

276+
# must specify if concat_dim='day' is not part of the vars
277+
kwargs = {"data_vars": "all"} if not include_day else {}
278+
276279
datasets = create_concat_datasets(
277280
len(vars_to_drop), seed=123, include_day=include_day
278281
)
279-
expected = concat(datasets, dim="day", data_vars="all")
282+
expected = concat(datasets, dim="day", **kwargs)
280283

281284
for i, name in enumerate(vars_to_drop):
282285
if include_day:
@@ -290,7 +293,7 @@ def test_concat_multiple_datasets_missing_vars(include_day: bool) -> None:
290293
for ds, varname in zip(datasets, vars_to_drop, strict=True)
291294
]
292295

293-
actual = concat(datasets, dim="day", data_vars="all")
296+
actual = concat(datasets, dim="day", **kwargs)
294297

295298
assert list(actual.data_vars.keys()) == [
296299
"pressure",
@@ -500,7 +503,7 @@ def rectify_dim_order(self, data: Dataset, dataset) -> Dataset:
500503
"dim,data", [["dim1", True], ["dim2", False]], indirect=["data"]
501504
)
502505
def test_concat_simple(self, data: Dataset, dim, coords) -> None:
503-
datasets = [g for _, g in data.groupby(dim, squeeze=False)]
506+
datasets = [g for _, g in data.groupby(dim)]
504507
assert_identical(data, concat(datasets, dim, coords=coords, compat="equals"))
505508

506509
def test_concat_merge_variables_present_in_some_datasets(
@@ -544,7 +547,7 @@ def test_concat_coords_kwarg(
544547
data = data.copy(deep=True)
545548
# make sure the coords argument behaves as expected
546549
data.coords["extra"] = ("dim4", np.arange(3))
547-
datasets = [g.squeeze() for _, g in data.groupby(dim, squeeze=False)]
550+
datasets = [g.squeeze() for _, g in data.groupby(dim)]
548551

549552
actual = concat(
550553
datasets, data[dim], coords=coords, data_vars="all", compat="equals"
@@ -900,6 +903,8 @@ def test_concat_promote_shape_for_scalars_with_mixed_lengths_along_concat_dim(
900903
actual = concat(objs, "x", coords="different", compat="equals")
901904
expected = Dataset({"x": [0, 1, 2]}, {"y": ("x", [-1, -2, -2])})
902905
assert_identical(actual, expected)
906+
actual = concat(objs, "x", coords="all")
907+
assert_identical(actual, expected)
903908

904909
def test_concat_promote_shape_broadcast_1d_x_1d_goes_to_2d(self) -> None:
905910
objs = [
@@ -995,8 +1000,8 @@ def test_concat_along_new_dim_multiindex(self) -> None:
9951000
@pytest.mark.parametrize("fill_value", [dtypes.NA, 2, 2.0, {"a": 2, "b": 1}])
9961001
def test_concat_fill_value(self, fill_value) -> None:
9971002
datasets = [
998-
Dataset({"a": ("x", [2, 3]), "b": ("x", [-2, 1]), "x": [1, 2]}),
999-
Dataset({"a": ("x", [1, 2]), "b": ("x", [3, -1]), "x": [0, 1]}),
1003+
Dataset({"a": ("x", [2, 3]), "b": ("x", [-2, 1])}, {"x": [1, 2]}),
1004+
Dataset({"a": ("x", [1, 2]), "b": ("x", [3, -1])}, {"x": [0, 1]}),
10001005
]
10011006
if fill_value == dtypes.NA:
10021007
# if we supply the default, we expect the missing value for a
@@ -1128,13 +1133,15 @@ def test_concat(self) -> None:
11281133
stacked = concat(grouped, pd.Index(ds["x"], name="x"))
11291134
assert_identical(foo, stacked)
11301135

1131-
actual2 = concat([foo[0], foo[1]], pd.Index([0, 1]), coords="all").reset_coords(
1132-
drop=True
1133-
)
1136+
actual2 = concat(
1137+
[foo.isel(x=0), foo.isel(x=1)], pd.Index([0, 1]), coords="all"
1138+
).reset_coords(drop=True)
11341139
expected = foo[:2].rename({"x": "concat_dim"})
11351140
assert_identical(expected, actual2)
11361141

1137-
actual3 = concat([foo[0], foo[1]], [0, 1], coords="all").reset_coords(drop=True)
1142+
actual3 = concat(
1143+
[foo.isel(x=0), foo.isel(x=1)], [0, 1], coords="all"
1144+
).reset_coords(drop=True)
11381145
expected = foo[:2].rename({"x": "concat_dim"})
11391146
assert_identical(expected, actual3)
11401147

@@ -1196,7 +1203,7 @@ def test_concat_avoids_index_auto_creation(self) -> None:
11961203
assert combined.indexes == {}
11971204

11981205
# should not raise on stack
1199-
combined = concat(arrays, dim="z", coords="different", compat="equals")
1206+
combined = concat(arrays, dim="z")
12001207
assert combined.shape == (2, 3, 3)
12011208
assert combined.dims == ("z", "x", "y")
12021209

xarray/tests/test_dataarray.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,10 +1453,11 @@ def test_concat_with_default_coords_warns(self) -> None:
14531453

14541454
with pytest.warns(FutureWarning):
14551455
original = xr.concat([da, db], dim="x")
1456+
assert original.y.size == 4
14561457
with set_options(use_new_combine_kwarg_defaults=True):
1458+
# default compat="minimal" will pick the first one
14571459
new = xr.concat([da, db], dim="x")
1458-
1459-
assert original.y.shape != new.y.shape
1460+
assert new.y.size == 1
14601461

14611462
def test_virtual_default_coords(self) -> None:
14621463
array = DataArray(np.zeros((5,)), dims="x")

xarray/util/deprecation_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ def warning_message(self, message: str, recommend_set_options: bool = True) -> s
206206

207207
def error_message(self) -> str:
208208
return (
209-
f" Error might be related to new default ({self._name}={self._new!r}). "
210-
f"Previously the default was {self._name}={self._old!r}. "
211-
f"The recommendation is to set {self._name} explicitly for this case."
209+
f" Error might be related to new default (`{self._name}={self._new!r}`). "
210+
f"Previously the default was `{self._name}={self._old!r}`. "
211+
f"The recommendation is to set {self._name!r} explicitly for this case."
212212
)
213213

214214

0 commit comments

Comments
 (0)