(arr, obj, values, axis)
| 2409 | |
| 2410 | @derived_from(np) |
| 2411 | def insert(arr, obj, values, axis): |
| 2412 | # axis is a required argument here to avoid needing to deal with the numpy |
| 2413 | # default case (which reshapes the array to make it flat) |
| 2414 | axis = validate_axis(axis, arr.ndim) |
| 2415 | |
| 2416 | if isinstance(obj, slice): |
| 2417 | obj = np.arange(*obj.indices(arr.shape[axis])) |
| 2418 | obj = np.asarray(obj) |
| 2419 | scalar_obj = obj.ndim == 0 |
| 2420 | if scalar_obj: |
| 2421 | obj = np.atleast_1d(obj) |
| 2422 | |
| 2423 | obj = np.where(obj < 0, obj + arr.shape[axis], obj) |
| 2424 | if (np.diff(obj) < 0).any(): |
| 2425 | raise NotImplementedError( |
| 2426 | "da.insert only implemented for monotonic ``obj`` argument" |
| 2427 | ) |
| 2428 | |
| 2429 | split_arr = split_at_breaks(arr, np.unique(obj), axis) |
| 2430 | |
| 2431 | if getattr(values, "ndim", 0) == 0: |
| 2432 | # we need to turn values into a dask array |
| 2433 | name = "values-" + tokenize(values) |
| 2434 | dtype = getattr(values, "dtype", type(values)) |
| 2435 | values = Array({(name,): values}, name, chunks=(), dtype=dtype) |
| 2436 | |
| 2437 | values_shape = tuple( |
| 2438 | len(obj) if axis == n else s for n, s in enumerate(arr.shape) |
| 2439 | ) |
| 2440 | values = broadcast_to(values, values_shape) |
| 2441 | elif scalar_obj: |
| 2442 | values = values[(slice(None),) * axis + (None,)] |
| 2443 | |
| 2444 | values_chunks = tuple( |
| 2445 | values_bd if axis == n else arr_bd |
| 2446 | for n, (arr_bd, values_bd) in enumerate(zip(arr.chunks, values.chunks)) |
| 2447 | ) |
| 2448 | values = values.rechunk(values_chunks) |
| 2449 | |
| 2450 | counts = np.bincount(obj)[:-1] |
| 2451 | values_breaks = np.cumsum(counts[counts > 0]) |
| 2452 | split_values = split_at_breaks(values, values_breaks, axis) |
| 2453 | |
| 2454 | interleaved = list(interleave([split_arr, split_values])) |
| 2455 | interleaved = [i for i in interleaved if i.nbytes] |
| 2456 | return concatenate(interleaved, axis=axis) |
| 2457 | |
| 2458 | |
| 2459 | @derived_from(np) |
nothing calls this directly
no test coverage detected
searching dependent graphs…