Skip to content

BUG: Applying np.fix on scalar returns 0-D array #8993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jakirkham opened this issue Apr 26, 2017 · 3 comments · Fixed by #8996
Closed

BUG: Applying np.fix on scalar returns 0-D array #8993

jakirkham opened this issue Apr 26, 2017 · 3 comments · Fixed by #8996

Comments

@jakirkham
Copy link
Contributor

jakirkham commented Apr 26, 2017

Appears that when NumPy's fix function is given a scalar, it returns a 0-D array as the result. However, the documentation clearly shows it returning a scalar when given a scalar. Either this is a bug or the documentation needs to be updated. IMHO having a scalar returned when giving a scalar seems desirable. Example code and conda environment details below.

Code sample:

In [1]: import numpy as np

In [2]: np.fix(3.14)
Out[2]: array(3.0)

In [3]: np.fix(3)
Out[3]: array(3.0)

In [4]: np.fix([2.1, 2.9, -2.1, -2.9])
Out[4]: array([ 2.,  2., -2., -2.])

Environment:

# environment.yml
name: test
channels: !!python/tuple
- !!python/unicode
  'conda-forge'
- !!python/unicode
  'defaults'
dependencies:
- conda-forge::appnope=0.1.0=py27_0
- conda-forge::backports.shutil_get_terminal_size=1.0.0=py27_1
- conda-forge::blas=1.1=openblas
- conda-forge::ca-certificates=2017.1.23=1
- conda-forge::certifi=2017.4.17=py27_0
- conda-forge::decorator=4.0.11=py27_0
- conda-forge::enum34=1.1.6=py27_1
- conda-forge::ipython=5.3.0=py27_0
- conda-forge::ipython_genutils=0.2.0=py27_0
- conda-forge::libgfortran=3.0.0=0
- conda-forge::ncurses=5.9=10
- conda-forge::numpy=1.12.1=py27_blas_openblas_200
- conda-forge::openblas=0.2.19=2
- conda-forge::openssl=1.0.2k=0
- conda-forge::pathlib2=2.2.1=py27_0
- conda-forge::pexpect=4.2.1=py27_0
- conda-forge::pickleshare=0.7.3=py27_0
- conda-forge::prompt_toolkit=1.0.14=py27_0
- conda-forge::ptyprocess=0.5.1=py27_0
- conda-forge::pygments=2.2.0=py27_0
- conda-forge::python=2.7.13=0
- conda-forge::readline=6.2=0
- conda-forge::scandir=1.5=py27_1
- conda-forge::setuptools=33.1.1=py27_0
- conda-forge::simplegeneric=0.8.1=py27_0
- conda-forge::six=1.10.0=py27_1
- conda-forge::sqlite=3.13.0=1
- conda-forge::tk=8.5.19=1
- conda-forge::traitlets=4.3.2=py27_0
- conda-forge::wcwidth=0.1.7=py27_0
- conda-forge::zlib=1.2.11=0

Note: Sorry if this is a duplicate issue. Searching the issue tracker for "fix" was not very effective.
@eric-wieser
Copy link
Member

eric-wieser commented Apr 26, 2017

Seems pretty dumb that that function:

Should be a trivial fix, changing the code to:

x = nx.asanyarray(x)
y1 = nx.floor(x)
y2 = nx.ceil(x)
res = nx.where(x >= 0, y1, y2)
if y is not None:
    ### todo: deprecate here
    y[...] = res
    return y
return res

@eric-wieser
Copy link
Member

eric-wieser commented Apr 26, 2017

Note that whatever fix is applied here, it should be applied to every function within that file - they all suffer from the same problem

@eric-wieser eric-wieser self-assigned this Apr 26, 2017
@eric-wieser
Copy link
Member

eric-wieser commented Apr 26, 2017

Hmm - this is maybe a bug in np.where as well (#8994)? That currently always returns a 0d array

@eric-wieser eric-wieser changed the title Applying fix on scalar returns 0-D array Applying np.fix on scalar returns 0-D array Apr 26, 2017
@eric-wieser eric-wieser changed the title Applying np.fix on scalar returns 0-D array BUG: Applying np.fix on scalar returns 0-D array Apr 26, 2017
eric-wieser added a commit to eric-wieser/numpy that referenced this issue Apr 26, 2017
No need to reinvent the wheel here - the ufunc machinery will handle the out arguments

Fixes numpy#8993
mherkazandjian pushed a commit to mherkazandjian/numpy that referenced this issue May 30, 2017
No need to reinvent the wheel here - the ufunc machinery will handle the out arguments

Fixes numpy#8993
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants