Skip to content

preserve chunked data when creating DataArray from DataArray #5984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 13, 2022
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Deprecations

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

Expand Down
4 changes: 3 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ def as_compatible_data(data, fastpath=False):

Finally, wrap it up with an adapter if necessary.
"""
from .dataarray import DataArray

if fastpath and getattr(data, "ndim", 0) > 0:
# can't use fastpath (yet) for scalars
return _maybe_wrap_data(data)

if isinstance(data, Variable):
if isinstance(data, (Variable, DataArray)):
return data.data

if isinstance(data, NON_NUMPY_SUPPORTED_ARRAY_TYPES):
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ReturnItem,
assert_allclose,
assert_array_equal,
assert_chunks_equal,
assert_equal,
assert_identical,
has_dask,
Expand Down Expand Up @@ -410,6 +411,19 @@ def test_constructor_from_self_described(self):
actual = DataArray(IndexVariable("foo", ["a", "b"]))
assert_identical(expected, actual)

@requires_dask
def test_constructor_from_self_described_chunked(self):
expected = DataArray(
[[-0.1, 21], [0, 2]],
coords={"x": ["a", "b"], "y": [-1, -2]},
dims=["x", "y"],
name="foobar",
attrs={"bar": 2},
).chunk()
actual = DataArray(expected)
assert_identical(expected, actual)
assert_chunks_equal(expected, actual)

def test_constructor_from_0d(self):
expected = Dataset({None: ([], 0)})[None]
actual = DataArray(0)
Expand Down