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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ 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)

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