Skip to content

Commit 3f3a197

Browse files
authored
Merge branch 'main' into groupby-aggs-using-numpy-groupies
2 parents c157fca + 4c865d6 commit 3f3a197

13 files changed

+36
-16
lines changed

Diff for: .pre-commit-config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ repos:
4545
types-PyYAML,
4646
types-pytz,
4747
typing-extensions==3.10.0.0,
48+
numpy,
4849
]
4950
# run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194
5051
# - repo: https://github.com/asottile/pyupgrade

Diff for: doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Deprecations
4646

4747
Bug fixes
4848
~~~~~~~~~
49+
- Preserve chunks when creating a :py:class:`DataArray` from another :py:class:`DataArray`
50+
(:pull:`5984`). By `Fabian Hofmann <https://github.com/FabianHofmann>`_.
4951
- Properly support :py:meth:`DataArray.ffill`, :py:meth:`DataArray.bfill`, :py:meth:`Dataset.ffill` and :py:meth:`Dataset.bfill` along chunked dimensions (:issue:`6112`).
5052
By `Joseph Nowak <https://github.com/josephnowak>`_.
5153

Diff for: xarray/backends/file_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import io
33
import threading
44
import warnings
5-
from typing import Any, Dict, cast
5+
from typing import Any, Dict
66

77
from ..core import utils
88
from ..core.options import OPTIONS
@@ -11,7 +11,7 @@
1111

1212
# Global cache for storing open files.
1313
FILE_CACHE: LRUCache[str, io.IOBase] = LRUCache(
14-
maxsize=cast(int, OPTIONS["file_cache_maxsize"]), on_evict=lambda k, v: v.close()
14+
maxsize=OPTIONS["file_cache_maxsize"], on_evict=lambda k, v: v.close()
1515
)
1616
assert FILE_CACHE.maxsize, "file cache must be at least size one"
1717

Diff for: xarray/core/accessor_str.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def func(x):
276276

277277
if isinstance(pat, np.ndarray):
278278
# apply_ufunc doesn't work for numpy arrays with output object dtypes
279-
func = np.vectorize(func)
280-
return func(pat)
279+
func_ = np.vectorize(func)
280+
return func_(pat)
281281
else:
282282
return _apply_str_ufunc(func=func, obj=pat, dtype=np.object_)
283283

Diff for: xarray/core/dataset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6920,9 +6920,9 @@ def polyfit(
69206920
if full:
69216921
rank = xr.DataArray(rank, name=xname + "matrix_rank")
69226922
variables[rank.name] = rank
6923-
sing = np.linalg.svd(lhs, compute_uv=False)
6923+
_sing = np.linalg.svd(lhs, compute_uv=False)
69246924
sing = xr.DataArray(
6925-
sing,
6925+
_sing,
69266926
dims=(degree_dim,),
69276927
coords={degree_dim: np.arange(rank - 1, -1, -1)},
69286928
name=xname + "singular_values",

Diff for: xarray/core/duck_array_ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from numpy import zeros_like # noqa
1717
from numpy import around, broadcast_to # noqa
1818
from numpy import concatenate as _concatenate
19-
from numpy import einsum, isclose, isin, isnan, isnat, pad # noqa
19+
from numpy import einsum, isclose, isin, isnan, isnat # noqa
2020
from numpy import stack as _stack
2121
from numpy import take, tensordot, transpose, unravel_index # noqa
2222
from numpy import where as _where
@@ -168,7 +168,7 @@ def cumulative_trapezoid(y, x, axis):
168168

169169
# Pad so that 'axis' has same length in result as it did in y
170170
pads = [(1, 0) if i == axis else (0, 0) for i in range(y.ndim)]
171-
integrand = pad(integrand, pads, mode="constant", constant_values=0.0)
171+
integrand = np.pad(integrand, pads, mode="constant", constant_values=0.0)
172172

173173
return cumsum(integrand, axis=axis, skipna=False)
174174

Diff for: xarray/core/formatting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def format_array_flat(array, max_width: int):
200200
(max_possibly_relevant < array.size) or (cum_len > max_width).any()
201201
):
202202
padding = " ... "
203-
max_len = max(int(np.argmax(cum_len + len(padding) - 1 > max_width)), 2) # type: ignore[type-var]
203+
max_len = max(int(np.argmax(cum_len + len(padding) - 1 > max_width)), 2)
204204
count = min(array.size, max_len)
205205
else:
206206
count = array.size

Diff for: xarray/core/indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ def _decompose_outer_indexer(
872872
backend_indexer: List[Any] = []
873873
np_indexer = []
874874
# make indexer positive
875-
pos_indexer = []
875+
pos_indexer: list[np.ndarray | int | np.number] = []
876876
for k, s in zip(indexer.tuple, shape):
877877
if isinstance(k, np.ndarray):
878878
pos_indexer.append(np.where(k < 0, k + s, k))

Diff for: xarray/core/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def read_magic_number_from_file(filename_or_obj, count=8) -> bytes:
652652
"file-like object read/write pointer not at the start of the file, "
653653
"please close and reopen, or use a context manager"
654654
)
655-
magic_number = filename_or_obj.read(count) # type: ignore
655+
magic_number = filename_or_obj.read(count)
656656
filename_or_obj.seek(0)
657657
else:
658658
raise TypeError(f"cannot read the magic number form {type(filename_or_obj)}")

Diff for: xarray/core/variable.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ def as_compatible_data(data, fastpath=False):
198198
199199
Finally, wrap it up with an adapter if necessary.
200200
"""
201+
from .dataarray import DataArray
202+
201203
if fastpath and getattr(data, "ndim", 0) > 0:
202204
# can't use fastpath (yet) for scalars
203205
return _maybe_wrap_data(data)
204206

205-
if isinstance(data, Variable):
207+
if isinstance(data, (Variable, DataArray)):
206208
return data.data
207209

208210
if isinstance(data, NON_NUMPY_SUPPORTED_ARRAY_TYPES):
@@ -1236,7 +1238,7 @@ def _shift_one_dim(self, dim, count, fill_value=dtypes.NA):
12361238
dim_pad = (width, 0) if count >= 0 else (0, width)
12371239
pads = [(0, 0) if d != dim else dim_pad for d in self.dims]
12381240

1239-
data = duck_array_ops.pad(
1241+
data = np.pad(
12401242
trimmed_data.astype(dtype),
12411243
pads,
12421244
mode="constant",
@@ -1376,7 +1378,7 @@ def pad(
13761378
if reflect_type is not None:
13771379
pad_option_kwargs["reflect_type"] = reflect_type # type: ignore[assignment]
13781380

1379-
array = duck_array_ops.pad(
1381+
array = np.pad( # type: ignore[call-overload]
13801382
self.data.astype(dtype, copy=False),
13811383
pad_width_by_index,
13821384
mode=mode,

Diff for: xarray/tests/test_computation.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1934,7 +1934,8 @@ def test_polyval(use_dask, use_datetime) -> None:
19341934
)
19351935
x = xr.core.missing.get_clean_interp_index(xcoord, "x")
19361936
else:
1937-
xcoord = x = np.arange(10)
1937+
x = np.arange(10)
1938+
xcoord = xr.DataArray(x, dims=("x",), name="x")
19381939

19391940
da = xr.DataArray(
19401941
np.stack((1.0 + x + 2.0 * x ** 2, 1.0 + 2.0 * x + 3.0 * x ** 2)),

Diff for: xarray/tests/test_conventions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def test_decode_cf_datetime_transition_to_invalid(self) -> None:
292292
warnings.filterwarnings("ignore", "unable to decode time")
293293
ds_decoded = conventions.decode_cf(ds)
294294

295-
expected = [datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)]
295+
expected = np.array([datetime(2000, 1, 1, 0, 0), datetime(2265, 10, 28, 0, 0)])
296296

297297
assert_array_equal(ds_decoded.time.values, expected)
298298

Diff for: xarray/tests/test_dataarray.py

+14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
ReturnItem,
3131
assert_allclose,
3232
assert_array_equal,
33+
assert_chunks_equal,
3334
assert_equal,
3435
assert_identical,
3536
has_dask,
@@ -410,6 +411,19 @@ def test_constructor_from_self_described(self):
410411
actual = DataArray(IndexVariable("foo", ["a", "b"]))
411412
assert_identical(expected, actual)
412413

414+
@requires_dask
415+
def test_constructor_from_self_described_chunked(self):
416+
expected = DataArray(
417+
[[-0.1, 21], [0, 2]],
418+
coords={"x": ["a", "b"], "y": [-1, -2]},
419+
dims=["x", "y"],
420+
name="foobar",
421+
attrs={"bar": 2},
422+
).chunk()
423+
actual = DataArray(expected)
424+
assert_identical(expected, actual)
425+
assert_chunks_equal(expected, actual)
426+
413427
def test_constructor_from_0d(self):
414428
expected = Dataset({None: ([], 0)})[None]
415429
actual = DataArray(0)

0 commit comments

Comments
 (0)