|
9 | 9 | import numpy as np
|
10 | 10 | from . import multiarray
|
11 | 11 | from .multiarray import (
|
12 |
| - ALLOW_THREADS, |
13 |
| - BUFSIZE, CLIP, MAXDIMS, MAY_SHARE_BOUNDS, MAY_SHARE_EXACT, RAISE, |
14 |
| - WRAP, arange, array, asarray, asanyarray, ascontiguousarray, |
15 |
| - asfortranarray, broadcast, can_cast, |
16 |
| - concatenate, copyto, dot, dtype, empty, |
17 |
| - empty_like, flatiter, frombuffer, from_dlpack, fromfile, fromiter, |
18 |
| - fromstring, inner, lexsort, matmul, may_share_memory, |
19 |
| - min_scalar_type, ndarray, nditer, nested_iters, promote_types, |
20 |
| - putmask, result_type, shares_memory, vdot, where, |
21 |
| - zeros, normalize_axis_index, _get_promotion_state, _set_promotion_state) |
| 12 | + ALLOW_THREADS, BUFSIZE, CLIP, MAXDIMS, MAY_SHARE_BOUNDS, MAY_SHARE_EXACT, |
| 13 | + RAISE, WRAP, arange, array, asarray, asanyarray, ascontiguousarray, |
| 14 | + asfortranarray, broadcast, can_cast, concatenate, copyto, dot, dtype, |
| 15 | + empty, empty_like, flatiter, frombuffer, from_dlpack, fromfile, fromiter, |
| 16 | + fromstring, inner, lexsort, matmul, may_share_memory, min_scalar_type, |
| 17 | + ndarray, nditer, nested_iters, promote_types, putmask, result_type, |
| 18 | + shares_memory, vdot, where, zeros, normalize_axis_index, |
| 19 | + _get_promotion_state, _set_promotion_state |
| 20 | +) |
22 | 21 |
|
23 | 22 | from . import overrides
|
24 | 23 | from . import umath
|
|
43 | 42 | 'arange', 'array', 'asarray', 'asanyarray', 'ascontiguousarray',
|
44 | 43 | 'asfortranarray', 'zeros', 'count_nonzero', 'empty', 'broadcast', 'dtype',
|
45 | 44 | 'fromstring', 'fromfile', 'frombuffer', 'from_dlpack', 'where',
|
46 |
| - 'argwhere', 'copyto', 'concatenate', 'lexsort', |
| 45 | + 'argwhere', 'copyto', 'concatenate', 'lexsort', 'astype', |
47 | 46 | 'can_cast', 'promote_types', 'min_scalar_type',
|
48 | 47 | 'result_type', 'isfortran', 'empty_like', 'zeros_like', 'ones_like',
|
49 | 48 | 'correlate', 'convolve', 'inner', 'dot', 'outer', 'vdot', 'roll',
|
@@ -2508,6 +2507,60 @@ def array_equiv(a1, a2):
|
2508 | 2507 | return bool(asarray(a1 == a2).all())
|
2509 | 2508 |
|
2510 | 2509 |
|
| 2510 | +def _astype_dispatcher(x, dtype, /, *, copy=None): |
| 2511 | + return (x, dtype) |
| 2512 | + |
| 2513 | + |
| 2514 | +@array_function_dispatch(_astype_dispatcher) |
| 2515 | +def astype(x, dtype, /, *, copy = True): |
| 2516 | + """ |
| 2517 | + Copies an array to a specified data type. |
| 2518 | +
|
| 2519 | + This function is an Array API compatible alternative to |
| 2520 | + `numpy.ndarray.astype`. |
| 2521 | +
|
| 2522 | + Parameters |
| 2523 | + ---------- |
| 2524 | + x : array_like |
| 2525 | + Input array to cast. |
| 2526 | + dtype : dtype |
| 2527 | + Data type of the result. |
| 2528 | + copy : bool, optional |
| 2529 | + Specifies whether to copy an array when the specified dtype matches |
| 2530 | + the data type of the input array ``x``. If ``True``, a newly allocated |
| 2531 | + array must always be returned. If ``False`` and the specified dtype |
| 2532 | + matches the data type of the input array, the input array must be |
| 2533 | + returned; otherwise, a newly allocated array must be returned. |
| 2534 | + Defaults to ``True``. |
| 2535 | +
|
| 2536 | + Returns |
| 2537 | + ------- |
| 2538 | + out : ndarray |
| 2539 | + An array having the specified data type. |
| 2540 | +
|
| 2541 | + See Also |
| 2542 | + -------- |
| 2543 | + ndarray.astype |
| 2544 | +
|
| 2545 | + Examples |
| 2546 | + -------- |
| 2547 | + >>> arr = np.array([1, 2, 3]); arr |
| 2548 | + array([1, 2, 3]) |
| 2549 | + >>> np.astype(arr, np.float64) |
| 2550 | + array([1., 2., 3.]) |
| 2551 | +
|
| 2552 | + Non-copy case: |
| 2553 | +
|
| 2554 | + >>> arr = np.array([1, 2, 3]) |
| 2555 | + >>> arr_noncpy = np.astype(arr, arr.dtype, copy=False) |
| 2556 | + >>> np.shares_memory(arr, arr_noncpy) |
| 2557 | + True |
| 2558 | +
|
| 2559 | + """ |
| 2560 | + x = asarray(x) |
| 2561 | + return x.astype(dtype, copy=copy) |
| 2562 | + |
| 2563 | + |
2511 | 2564 | inf = PINF
|
2512 | 2565 | nan = NAN
|
2513 | 2566 | False_ = bool_(False)
|
|
0 commit comments