You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add specification for explicitly casting an array to a specified dtype (#290)
* Add astype API and restrict asarray to type promotion rules
* Update copy
* Update copy
* Add support for a `copy` keyword
* Restrict `docutils` version
-Object to be converted to an array. Can be a Python scalar, a (possibly nested) sequence of Python scalars, or an object supporting DLPack or the Python buffer protocol.
62
+
-object to be converted to an array. May be a Python scalar, a (possibly nested) sequence of Python scalars, or an object supporting DLPack or the Python buffer protocol.
63
63
64
64
:::{tip}
65
65
An object supporting DLPack has `__dlpack__` and `__dlpack_device__` methods.
@@ -68,21 +68,33 @@ Convert the input to an array.
68
68
69
69
-**dtype**: _Optional\[<dtype>]_
70
70
71
-
- output array data type. If `dtype` is `None`, the output array data type must be inferred from the data type(s) in `obj`. If all input values are Python scalars, then if they're all `bool` the output dtype will be `bool`; if they're a mix of `bool`s and `int` the output dtype will be the default integer data type; if they contain `float`s the output dtype will be the default floating-point data type. Default: `None`.
71
+
- output array data type. If `dtype` is `None`, the output array data type must be inferred from the data type(s) in `obj`. If all input values are Python scalars, then
72
+
73
+
- if all values are of type `bool`, the output data type must be `bool`.
74
+
- if the values are a mixture of `bool`s and `int`, the output data type must be the default integer data type.
75
+
- if one or more values are `float`s, the output data type must be the default floating-point data type.
76
+
77
+
Default: `None`.
78
+
79
+
```{note}
80
+
If `dtype` is not `None`, then array conversions should obey {ref}`type-promotion` rules. Conversions not specified according to {ref}`type-promotion` rules may or may not be permitted by a conforming array library.
81
+
82
+
To perform an explicit cast, use {ref}`function-astype`.
83
+
```
72
84
73
85
- **device**: _Optional\[ <device> ]_
74
86
75
87
- device on which to place the created array. If `device` is `None` and `x` is either an array or an object supporting DLPack, the output array device must be inferred from `x`. Default: `None`.
76
88
77
89
- **copy**: _Optional\[ bool ]_
78
90
79
-
-Whether or not to make a copy of the input. If `True`, always copies. If `False`, never copies for input which supports DLPack or the buffer protocol, and raises `ValueError` in case that would be necessary. If `None`, reuses existing memory buffer if possible, copies otherwise. Default: `None`.
91
+
- boolean indicating whether or not to copy the input. If `True`, the function must always copy. If `False`, the function must never copy for input which supports DLPack or the buffer protocol and must raise a `ValueError` in case a copy would be necessary. If `None`, the function must reuse existing memory buffer if possible and copy otherwise. Default: `None`.
Copy file name to clipboardExpand all lines: spec/API_specification/data_type_functions.md
+30
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,36 @@ A conforming implementation of the array API standard must provide and support t
7
7
<!-- NOTE: please keep the constants in alphabetical order -->
8
8
9
9
## Objects in API
10
+
11
+
(function-astype)=
12
+
### astype(x, dtype, /, *, copy=True)
13
+
14
+
Copies an array to a specified data type irrespective of {ref}`type-promotion` rules.
15
+
16
+
```{note}
17
+
Casting floating-point `NaN` and `infinity` values to integral data types is not specified and is implementation-dependent.
18
+
```
19
+
20
+
#### Parameters
21
+
22
+
-**x**: _<array>_
23
+
24
+
- array to cast.
25
+
26
+
-**dtype**: _<dtype>_
27
+
28
+
- desired data type.
29
+
30
+
-**copy**: _<bool>_
31
+
32
+
- specifies whether to copy an array when the specified `dtype` matches the data type of the input array `x`. If `True`, a newly allocated array must always be returned. If `False` and the specified `dtype` matches the data type of the input array, the input array must be returned; otherwise, a newly allocated must be returned. Default: `True`.
33
+
34
+
#### Returns
35
+
36
+
-**out**: _<array>_
37
+
38
+
- an array having the specified data type. The returned array must have the same shape as `x`.
0 commit comments