Skip to content

Commit 2461de3

Browse files
authored
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
1 parent f267ebd commit 2461de3

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ sphinx-material==0.0.30
33
myst-parser
44
sphinx_markdown_tables
55
sphinx_copybutton
6+
docutils<0.18

spec/API_specification/creation_functions.md

+16-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Convert the input to an array.
5959

6060
- **obj**: _Union\[ &lt;array&gt;, bool, int, float, NestedSequence\[ bool | int | float ], SupportsDLPack, SupportsBufferProtocol ]_
6161

62-
- 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.
6363

6464
:::{tip}
6565
An object supporting DLPack has `__dlpack__` and `__dlpack_device__` methods.
@@ -68,21 +68,33 @@ Convert the input to an array.
6868

6969
- **dtype**: _Optional\[ &lt;dtype&gt; ]_
7070

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+
```
7284
7385
- **device**: _Optional\[ &lt;device&gt; ]_
7486
7587
- 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`.
7688
7789
- **copy**: _Optional\[ bool ]_
7890
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`.
8092
8193
#### Returns
8294
8395
- **out**: _&lt;array&gt;_
8496
85-
- An array containing the data from `obj`.
97+
- an array containing the data from `obj`.
8698
8799
88100
(function-empty)=

spec/API_specification/data_type_functions.md

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,36 @@ A conforming implementation of the array API standard must provide and support t
77
<!-- NOTE: please keep the constants in alphabetical order -->
88

99
## 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**: _&lt;array&gt;_
23+
24+
- array to cast.
25+
26+
- **dtype**: _&lt;dtype&gt;_
27+
28+
- desired data type.
29+
30+
- **copy**: _&lt;bool&gt;_
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**: _&lt;array&gt;_
37+
38+
- an array having the specified data type. The returned array must have the same shape as `x`.
39+
1040
(function-broadcast_arrays)=
1141
### broadcast_arrays(*arrays)
1242

0 commit comments

Comments
 (0)