question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explicit array conversion (e.g., array(), asarray())

See original GitHub issue

Reading 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:22 (22 by maintainers)

github_iconTop GitHub Comments

3reactions
rgommerscommented, Feb 1, 2021

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).

3reactions
rgommerscommented, Feb 1, 2021

Makes sense. We need only one function I think - asarray and array are almost identical. I’d suggest a mash-up, because neither is ideal:

asarray(obj, dtype=None, copy=False, order='K', ndmin=0)

Some thoughts:

  • subok as a keyword doesn’t make too much sense to me, subclasses should be fine if the library allows them.
  • order and ndmin make sense for asarray too even though numpy doesn’t have them in asarray.
  • what inputs types obj accepts should be very well specified, and stricter than what numpy does. Probably: sequences, generators, and anything with a __dlpack__ method.
  • Anything that doesn’t produce a supported dtype should raise an exception.
  • No buffer protocol, __array__ or __array_interface__.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found