From 6229ae26e6a98a0a4a2adb66d6e1a3f7305e6c41 Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Wed, 27 Dec 2023 15:45:16 -0600 Subject: [PATCH] Data type functions: astype. --- src/ragged/common/__init__.py | 5 +++++ src/ragged/common/_datatype.py | 38 +++++++++++++++++++++++++++++++++ src/ragged/v202212/__init__.py | 5 +++++ src/ragged/v202212/_datatype.py | 15 +++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 src/ragged/common/_datatype.py create mode 100644 src/ragged/v202212/_datatype.py diff --git a/src/ragged/common/__init__.py b/src/ragged/common/__init__.py index 84a1624..e94a79c 100644 --- a/src/ragged/common/__init__.py +++ b/src/ragged/common/__init__.py @@ -27,6 +27,9 @@ zeros, zeros_like, ) +from ._datatype import ( + astype, +) from ._obj import array __all__ = [ @@ -47,6 +50,8 @@ "triu", "zeros", "zeros_like", + # _datatype + "astype", # _obj "array", ] diff --git a/src/ragged/common/_datatype.py b/src/ragged/common/_datatype.py new file mode 100644 index 0000000..6dcaa84 --- /dev/null +++ b/src/ragged/common/_datatype.py @@ -0,0 +1,38 @@ +# BSD 3-Clause License; see https://github.com/scikit-hep/ragged/blob/main/LICENSE + +""" +https://data-apis.org/array-api/latest/API_specification/data_type_functions.html +""" + +from __future__ import annotations + +from ._obj import array +from ._typing import ( + Dtype, +) + + +def astype(x: array, dtype: Dtype, /, *, copy: bool = True) -> array: + """ + Copies an array to a specified data type irrespective of type promotion rules. + + Args: + x: Array to cast. + dtype: Desired data type. + copy: 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 is always returned. If `False` and the specified + `dtype` matches the data type of the input array, the input array + is returned; otherwise, a newly allocated array is returned. + + Returns: + An array having the specified data type. The returned array has the + same `shape` as `x`. + + https://data-apis.org/array-api/latest/API_specification/generated/array_api.astype.html + """ + + assert x, "TODO" + assert dtype, "TODO" + assert copy, "TODO" + assert False, "TODO" diff --git a/src/ragged/v202212/__init__.py b/src/ragged/v202212/__init__.py index 5906977..7e1ec84 100644 --- a/src/ragged/v202212/__init__.py +++ b/src/ragged/v202212/__init__.py @@ -29,6 +29,9 @@ zeros, zeros_like, ) +from ._datatype import ( + astype, +) from ._obj import array __all__ = [ @@ -49,6 +52,8 @@ "triu", "zeros", "zeros_like", + # _datatype + "astype", # _obj "array", ] diff --git a/src/ragged/v202212/_datatype.py b/src/ragged/v202212/_datatype.py new file mode 100644 index 0000000..eff6f05 --- /dev/null +++ b/src/ragged/v202212/_datatype.py @@ -0,0 +1,15 @@ +# BSD 3-Clause License; see https://github.com/scikit-hep/ragged/blob/main/LICENSE + +""" +https://data-apis.org/array-api/2022.12/API_specification/creation_functions.html +""" + +from __future__ import annotations + +from ..common._datatype import ( + astype, +) + +__all__ = [ + "astype", +]