Skip to content
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

Enable __setitem__ for dask versions that support it. #5174

Merged
merged 27 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
14d18a7
Enable __setitem__ for dask versions that support it.
tammasloughran Apr 16, 2021
2e6b196
Cleanup
tammasloughran Apr 16, 2021
54aaa73
Dask is soft dependency
tammasloughran Apr 16, 2021
6ddb76c
Test for item assignment in dask
tammasloughran Apr 19, 2021
d75cd17
Fix minor mistakes
tammasloughran Apr 19, 2021
1bddcdd
raises_regex typo
tammasloughran Apr 19, 2021
0b5c7ad
Use assert_identical instead of assert
tammasloughran Apr 19, 2021
e725143
Update exception text
tammasloughran Apr 19, 2021
c84f774
tests/test_dask.py
tammasloughran Apr 19, 2021
f7d7bc4
Moved indexing tests to test_dask.py
tammasloughran Apr 19, 2021
63b4429
Code cleanup
tammasloughran Apr 19, 2021
b9768ce
More cleanup
tammasloughran Apr 19, 2021
777faaa
OuterIndexer not fully supported by dask. only use one array non-slic…
tammasloughran Apr 19, 2021
b4cb7d9
Cleanup again
tammasloughran Apr 19, 2021
b9a697e
Removed duplicate line
tammasloughran Apr 19, 2021
d141304
Conflict correction
tammasloughran Apr 20, 2021
8533072
Merge branch 'master' into master
tammasloughran Apr 20, 2021
3b1dbd3
Style corrections
tammasloughran Apr 20, 2021
bedfdf7
Merge branch 'master' of https://github.com/tammasloughran/xarray
tammasloughran Apr 20, 2021
c169fd0
pytest.raises not raises_regex
tammasloughran Apr 20, 2021
ee6efd0
Update xarray/core/indexing.py
tammasloughran Apr 21, 2021
9bc9b63
Update xarray/core/indexing.py
tammasloughran Apr 21, 2021
ee11d4e
New line
tammasloughran Apr 23, 2021
ca84825
mypy complains of type inconsistency for DASK_VERSION = None. Setting…
tammasloughran Apr 23, 2021
ae2a21f
Change ' to "
tammasloughran Apr 30, 2021
73bbadc
Added TODO and whats-new
tammasloughran May 1, 2021
153a7f3
Merge branch 'master' into master
tammasloughran May 1, 2021
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
28 changes: 21 additions & 7 deletions xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
from collections import defaultdict
from contextlib import suppress
from datetime import timedelta
from distutils.version import LooseVersion
from typing import Any, Callable, Iterable, List, Sequence, Tuple, Union

import numpy as np
import pandas as pd

try:
from dask import __version__ as dask_version
except ModuleNotFoundError:
pass

tammasloughran marked this conversation as resolved.
Show resolved Hide resolved
from . import duck_array_ops, nputils, utils
from .npcompat import DTypeLike
from .pycompat import (
Expand Down Expand Up @@ -1380,13 +1386,21 @@ def __getitem__(self, key):
return value

def __setitem__(self, key, value):
raise TypeError(
"this variable's data is stored in a dask array, "
"which does not support item assignment. To "
"assign to this variable, you must first load it "
"into memory explicitly using the .load() "
"method or accessing its .values attribute."
)
if LooseVersion(dask_version) >= LooseVersion("2021.04.0+17"):
tammasloughran marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(key, BasicIndexer):
self.array[key.tuple] = value
elif isinstance(key, VectorizedIndexer):
self.array.vindex[key.tuple] = value
elif isinstance(key, OuterIndexer):
self.array[key.tuple] = value
max-sixty marked this conversation as resolved.
Show resolved Hide resolved
else:
raise TypeError(
"This variable's data is stored in a dask array, "
"and the installed dask version does not support item "
"assignment. To assign to this variable, you must either upgrade dask or"
"first load the variable into memory explicitly using the .load() "
"method or accessing its .values attribute."
)

def transpose(self, order):
return self.array.transpose(order)
Expand Down
34 changes: 32 additions & 2 deletions xarray/tests/test_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,38 @@ def test_indexing(self):
self.assertLazyAndIdentical(u[0], v[0])
self.assertLazyAndIdentical(u[:1], v[:1])
self.assertLazyAndIdentical(u[[0, 1], [0, 1, 2]], v[[0, 1], [0, 1, 2]])
with raises_regex(TypeError, "stored in a dask array"):
v[:1] = 0
if LooseVersion(dask.__version__) >= LooseVersion("2021.04.0+17"):
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([99, 2, 3, 4]))
arr[0] = 99
assert_identical(arr, expected)
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([99, 99, 99, 4]))
arr[2::-1] = 99
assert_identical(arr, expected)
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([99, 99, 3, 99]))
arr[[0, -1, 1]] = 99
assert_identical(arr, expected)
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([99, 99, 99, 4]))
arr[np.arange(3)] = 99
assert_identical(arr, expected)
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([1, 99, 99, 99]))
arr[[False, True, True, True]] = 99
assert_identical(arr, expected)
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([1, 99, 99, 99]))
arr[np.arange(4) > 0] = 99
assert_identical(arr, expected)
arr = Variable(("x"), da.array([1, 2, 3, 4]))
expected = Variable(("x"), da.array([99, 99, 99, 99]))
arr[arr > 0] = 99
assert_identical(arr, expected)
else:
with raises_regex(TypeError, "stored in a dask array"):
v[:1] = 0

def test_squeeze(self):
u = self.eager_var
Expand Down