Explicit array conversion (e.g., array(), asarray())
See original GitHub issueReading through the standard, it appears that we may have missed an important feature: the ability to explicit coerce objects into a desired array type, either from builtin Python types like float/list or other array libraries. In other words, we need something like NumPy’s array()
and/or asarray()
functions.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:22 (22 by maintainers)
Top Results From Across the Web
numpy.asarray — NumPy v1.24 Manual
Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples...
Read more >Convert a PHP object to an associative array - Stack Overflow
Just typecast it $array = (array) $yourObject;. From Arrays: If an object is converted to an array, the result is an array whose...
Read more >C# | How to convert an ArrayList to Array - GeeksforGeeks
For conversion, you have to use ToArray(Type) method along with typeof keyword. And then you have to explicitly cast it to the specified...
Read more >Casting array elements - Oracle Communities
Is there any way to cast an entire array to the required type, instead of having to loop through and cast each element...
Read more >Chapter 4. NumPy Basics: Arrays and Vectorized Computation
Convert input data (list, tuple, array, or other sequence type) to an ndarray either by inferring a dtype or explicitly specifying a dtype....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Got a good answer thanks to Pearu: using
memoryview(x)
in Python will work if and only if an object supports the buffer protocol. So it does not require the Python C API. That makes it less onerous to support, at least in principle (in practice, all libraries will implement it via Python C API calls I’d expect).Makes sense. We need only one function I think -
asarray
andarray
are almost identical. I’d suggest a mash-up, because neither is ideal:Some thoughts:
subok
as a keyword doesn’t make too much sense to me, subclasses should be fine if the library allows them.order
andndmin
make sense forasarray
too even though numpy doesn’t have them inasarray
.obj
accepts should be very well specified, and stricter than what numpy does. Probably: sequences, generators, and anything with a__dlpack__
method.__array__
or__array_interface__
.