Skip to content

Commit 0259063

Browse files
Fabian HofmannIllviljanmathause
authored
preserve chunked data when creating DataArray from DataArray (#5984)
* variable: add case for data is DataArray instance in `as_compatible_data` test: add test for preserving chunks DataArray from DataArray creation * Update xarray/tests/test_dataarray.py Co-authored-by: Illviljan <[email protected]> * tests/test_dataarray: check equal chunks in `test_constructor_from_self_described_chunked` * add whats-new entry * Update whats-new.rst Co-authored-by: Illviljan <[email protected]> Co-authored-by: Mathias Hauser <[email protected]>
1 parent bc28eda commit 0259063

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
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

xarray/core/variable.py

Lines changed: 3 additions & 1 deletion
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):

xarray/tests/test_dataarray.py

Lines changed: 14 additions & 0 deletions
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)