@@ -129,24 +129,28 @@ def asarray(
129129 See the corresponding documentation in the array library and/or the array API
130130 specification for more details.
131131 """
132+ if isinstance (obj , da .Array ):
133+ if dtype is not None :
134+ # Note: at the moment of writing, dask ignores the copy parameter
135+ # and always behaves with copy=False. We pass the parameter anyway
136+ # for the sake of forward compatibility.
137+ res = obj .astype (dtype , copy = True if copy is True else False )
138+ if copy is False and res is not obj :
139+ raise ValueError ("Unable to avoid copy" )
140+ else :
141+ res = obj
142+ return obj .copy () if copy else obj
143+
132144 if copy is False :
133- # copy=False is not yet implemented in dask
134- raise NotImplementedError ("copy=False is not yet implemented" )
135- elif copy is True :
136- if isinstance (obj , da .Array ) and dtype is None :
137- return obj .copy ()
138- # Go through numpy, since dask copy is no-op by default
139- obj = np .array (obj , dtype = dtype , copy = True )
140- return da .array (obj , dtype = dtype )
141- else :
142- if not isinstance (obj , da .Array ) or dtype is not None and obj .dtype != dtype :
143- # copy=True to be uniform across dask < 2024.12 and >= 2024.12
144- # see https://github.com/dask/dask/pull/11524/
145- obj = np .array (obj , dtype = dtype , copy = True )
146- return da .from_array (obj )
147- return obj
148-
149- return da .asarray (obj , dtype = dtype , ** kwargs )
145+ raise NotImplementedError (
146+ "copy=False is not possible when converting a non-dask object to dask"
147+ )
148+
149+ # copy=None to be uniform across dask < 2024.12 and >= 2024.12
150+ # see https://github.com/dask/dask/pull/11524/
151+ obj = np .asarray (obj , dtype = dtype , copy = True )
152+ return da .from_array (obj )
153+
150154
151155from dask .array import (
152156 # Element wise aliases
0 commit comments