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.

BUG: ascontiguousarray and asfortranarray make 0d scalars into 1d arrays

See original GitHub issue

Not sure if this is intended behavior or a bug, but I had been under the assumption that the result of np.ascontiguousarray would be the same shape as the input. This is true except in the case of scalars.

>>> scalar = np.float64(3.0)
>>> np.asarray(scalar).shape  # asarray doesn't change shape
()
>>> np.ascontiguousarray(scalar).shape  # ascontiguousarray does
(1,)

If this is what should be expected, it would be helpful to document this in its docstring.

Thanks!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:19 (17 by maintainers)

github_iconTop GitHub Comments

3reactions
eric-wiesercommented, Nov 15, 2017

How about we just deprecate np.asfortranarray and np.ascontiguousarray in favor of np.asarray(..., order='C') and np.asarray(..., order='F'), since the former are:

  • More verbose
  • Both broken with respect to their documentation on 0d arrays
1reaction
charriscommented, Jun 21, 2015

Thinking more, I suspect this is intended for producing arrays for C and, in the case of asfortrancontiguous, for Fortran functions. Allowing zero dimensional arrays might well cause problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

is there a pythonic way to change scalar and 0d-array to 1d ...
A quick way to change a scalar or a 0d array to a 1d array using np.reshape after checking the dimension with np.ndim...
Read more >
numpy.ascontiguousarray — NumPy v1.24 Manual
In this case, it ensures the creation of an array object compatible with that passed in via this argument. New in version 1.20.0....
Read more >
Python usage notes - Numpy, scipy - Helpful
Generating 1D arrays. Your options to generate such matrices include: numpy.arange(). , similar to python's range() but produces a numpy array.
Read more >
NumPy Documentation — NumPy v1.19 Manual
How to convert a 1D array into a 2D array (how to add a new axis to an array) · Indexing and slicing...
Read more >
arrays - astro.udec.cl
Copies the first data point of the array to a standard Python scalar and ... Takes a sequence of 1D arrays and stacks...
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