0

I read the documentation but after reading that I still don't really understand the differences between the functions array(), asarray() and asanarray().

What are the differnces between these three and when do you use which one?

Edit: Was marked as a duplicate however asanarray() is not mentioned in the link to the so called duplicate. In other words, it's not a duplicate of that question.

Piet Hein
  • 184
  • 2
  • 16
  • You mean as [`asanyarray`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.asanyarray.html) for the last one? You may refer to the docs since it's not explained in the dupe target. – Moses Koledoye Aug 30 '18 at 18:05
  • Should indeed be asanyarray(). I got no clue what they mean with "but pass ndarray subclasses through". – Piet Hein Aug 30 '18 at 18:21
  • 1
    @PietHein That's explained in comments on the accepted answer: if you pass a, e.g., an `np.matrix` to `asarray`, it will be copied (because `matrix` is not the same thing as `ndarray`), but if you pass it to `asanyarray`, it will not be copied (because `matrix` _is_ a subclass of `ndarray`). – abarnert Aug 30 '18 at 18:27
  • We could either edit unutbu's answer to that question, or add a new one, to make it a broader canonical answer. The problem is that there's really no good reason to ask about `asanyarray` without also asking about `asfortranarray`, `ascontiguousarray`, `asarray_chkfinite`, and all of the other variants (and maybe even `fromiter`), and covering all of them would basically mean reiterating the whole documentation, which would ruin the simplicity of the existing answer. – abarnert Aug 30 '18 at 18:29
  • 1
    `np.asanyarray` just adds the `subok=True` parameter (look at its code). Unless you are familiar with subclasses like `np.matrix` and `np.recarray`, or need to write code that gracefully handles those, you probably don't need to understand the details (I don't). – hpaulj Aug 30 '18 at 18:57
  • 1
    I added a new answer that covers all of them in one line apiece; hopefully that's helpful. – abarnert Aug 30 '18 at 19:06

0 Answers0