(arr, obj, values, axis)
| 2361 | |
| 2362 | @derived_from(np) |
| 2363 | def insert(arr, obj, values, axis): |
| 2364 | # axis is a required argument here to avoid needing to deal with the numpy |
| 2365 | # default case (which reshapes the array to make it flat) |
| 2366 | axis = validate_axis(axis, arr.ndim) |
| 2367 | |
| 2368 | if isinstance(obj, slice): |
| 2369 | obj = np.arange(*obj.indices(arr.shape[axis])) |
| 2370 | obj = np.asarray(obj) |
| 2371 | scalar_obj = obj.ndim == 0 |
| 2372 | if scalar_obj: |
| 2373 | obj = np.atleast_1d(obj) |
| 2374 | |
| 2375 | obj = np.where(obj < 0, obj + arr.shape[axis], obj) |
| 2376 | if (np.diff(obj) < 0).any(): |
| 2377 | raise NotImplementedError( |
| 2378 | "da.insert only implemented for monotonic ``obj`` argument" |
| 2379 | ) |
| 2380 | |
| 2381 | split_arr = split_at_breaks(arr, np.unique(obj), axis) |
| 2382 | |
| 2383 | if getattr(values, "ndim", 0) == 0: |
| 2384 | # we need to turn values into a dask array |
| 2385 | name = "values-" + tokenize(values) |
| 2386 | dtype = getattr(values, "dtype", type(values)) |
| 2387 | values = Array({(name,): values}, name, chunks=(), dtype=dtype) |
| 2388 | |
| 2389 | values_shape = tuple( |
| 2390 | len(obj) if axis == n else s for n, s in enumerate(arr.shape) |
| 2391 | ) |
| 2392 | values = broadcast_to(values, values_shape) |
| 2393 | elif scalar_obj: |
| 2394 | values = values[(slice(None),) * axis + (None,)] |
| 2395 | |
| 2396 | values_chunks = tuple( |
| 2397 | values_bd if axis == n else arr_bd |
| 2398 | for n, (arr_bd, values_bd) in enumerate(zip(arr.chunks, values.chunks)) |
| 2399 | ) |
| 2400 | values = values.rechunk(values_chunks) |
| 2401 | |
| 2402 | counts = np.bincount(obj)[:-1] |
| 2403 | values_breaks = np.cumsum(counts[counts > 0]) |
| 2404 | split_values = split_at_breaks(values, values_breaks, axis) |
| 2405 | |
| 2406 | interleaved = list(interleave([split_arr, split_values])) |
| 2407 | interleaved = [i for i in interleaved if i.nbytes] |
| 2408 | return concatenate(interleaved, axis=axis) |
| 2409 | |
| 2410 | |
| 2411 | @derived_from(np) |
nothing calls this directly
no test coverage detected