Skip to content

Commit 377940b

Browse files
Make sure 'order' kwarg will not crash the 'astype' method in dask (#9317)
Allows the `order` kwarg to be passed in to the dask `astype` method without triggering an error. Our friends over at zarr have found a small bug in dask. While the numpy `astype` method allows the user to use an `order` keyword argument ([docs here](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html)), the corresponding dask `astype` method produces an error Why we should do this: 1. It's not always as obvious as telling the user to edit the line in their code that uses `astype`, since it's often used very indirectly. Here's one example: zarr-developers/zarr-python#962 (comment) 2. It reflects better on dask not to have odd errors popping up, even if this PR won't completely solve the issue being discussed over at zarr zarr-developers/zarr-python#962
1 parent e61405c commit 377940b

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

dask/array/core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,7 +2233,14 @@ def astype(self, dtype, **kwargs):
22332233
By default, astype always returns a newly allocated array. If this
22342234
is set to False and the `dtype` requirement is satisfied, the input
22352235
array is returned instead of a copy.
2236+
2237+
.. note::
2238+
2239+
Dask does not respect the contiguous memory layout of the array,
2240+
and will ignore the ``order`` keyword argument.
2241+
The default order is 'C' contiguous.
22362242
"""
2243+
kwargs.pop("order", None) # `order` is not respected, so we remove this kwarg
22372244
# Scalars don't take `casting` or `copy` kwargs - as such we only pass
22382245
# them to `map_blocks` if specified by user (different than defaults).
22392246
extra = set(kwargs) - {"casting", "copy"}

dask/array/tests/test_array_core.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,34 @@ def test_astype():
22862286
assert d.astype("f8") is d
22872287

22882288

2289+
def test_astype_gh1151():
2290+
a = np.arange(5).astype(np.int32)
2291+
b = da.from_array(a, (1,))
2292+
assert_eq(a.astype(np.int16), b.astype(np.int16))
2293+
2294+
2295+
def test_astype_gh9318():
2296+
# `order`` kwarg in `astype` should not cause an error
2297+
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], order="C")
2298+
b = da.from_array(a, chunks=(2, 2))
2299+
result_a = a.astype(float, order="F")
2300+
result_b = b.astype(float, order="F") # if no error at this line, pytest passes
2301+
assert_eq(result_a, result_b) # won't check the order matches, but checks results
2302+
2303+
2304+
@pytest.mark.xfail(reason="Github issue https://github.com/dask/dask/issues/9316")
2305+
def test_astype_gh9316():
2306+
# Issue https://github.com/dask/dask/issues/9316
2307+
# Can be combined with test_astype_gh9318 above when XFAIL marker is removed
2308+
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], order="C")
2309+
b = da.from_array(a, chunks=(2, 2))
2310+
result_a = a.astype(float, order="F")
2311+
result_b = b.astype(float, order="F")
2312+
result_b = result_b.compute()
2313+
assert result_a.flags.c_contiguous == result_b.flags.c_contiguous
2314+
assert result_a.flags.f_contiguous == result_b.flags.f_contiguous
2315+
2316+
22892317
def test_arithmetic():
22902318
x = np.arange(5).astype("f4") + 2
22912319
y = np.arange(5).astype("i8") + 2
@@ -3569,12 +3597,6 @@ def test_npartitions():
35693597
assert da.ones((5, 5), chunks=(2, 3)).npartitions == 6
35703598

35713599

3572-
def test_astype_gh1151():
3573-
a = np.arange(5).astype(np.int32)
3574-
b = da.from_array(a, (1,))
3575-
assert_eq(a.astype(np.int16), b.astype(np.int16))
3576-
3577-
35783600
def test_elemwise_name():
35793601
assert (da.ones(5, chunks=2) + 1).name.startswith("add-")
35803602

0 commit comments

Comments
 (0)