Thanks to visit codestin.com
Credit goes to github.com

Skip to content

nonzero on arrays of characters with whitespace #9462

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 Jul 26, 2017 · 6 comments
Closed

nonzero on arrays of characters with whitespace #9462

jakirkham opened this issue Jul 26, 2017 · 6 comments

Comments

@jakirkham
Copy link
Contributor

It seems that whitespace in a character array is treated as if it is zero. An example of this is shown below with details about the environment. This seems to match up with how character arrays work. Still this seems a bit surprising from a Python perspective. Am trying to understand if this is intentional and also if there is an builtin element-wise operator that we can use to reproduce this behavior.

Code snippet:

In [1]: import numpy as np

In [2]: s = np.array(list("hello world"))

In [3]: s
Out[3]: 
array(['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'],
      dtype='|S1')

In [4]: s.nonzero()
Out[4]: (array([ 0,  1,  2,  3,  4,  6,  7,  8,  9, 10]),)

Environment:

name: np2
channels:
- conda-forge
- defaults
dependencies:
- appnope=0.1.0=py27_0
- backports.shutil_get_terminal_size=1.0.0=py27_1
- blas=1.1=openblas
- ca-certificates=2017.4.17=0
- certifi=2017.4.17=py27_0
- decorator=4.1.1=py27_0
- enum34=1.1.6=py27_1
- ipython=5.4.1=py27_0
- ipython_genutils=0.2.0=py27_0
- libgfortran=3.0.0=0
- ncurses=5.9=10
- numpy=1.13.1=py27_blas_openblas_200
- openblas=0.2.19=2
- openssl=1.0.2l=0
- pathlib2=2.3.0=py27_0
- pexpect=4.2.1=py27_0
- pickleshare=0.7.3=py27_0
- pip=9.0.1=py27_0
- prompt_toolkit=1.0.14=py27_0
- ptyprocess=0.5.2=py27_0
- pygments=2.2.0=py27_0
- python=2.7.13=1
- readline=6.2=0
- scandir=1.5=py27_1
- setuptools=36.2.2=py27_0
- simplegeneric=0.8.1=py27_0
- six=1.10.0=py27_1
- sqlite=3.13.0=1
- tk=8.5.19=1
- traitlets=4.3.2=py27_0
- wcwidth=0.1.7=py27_0
- wheel=0.29.0=py27_0
- zlib=1.2.11=0
- pip:
  - backports.shutil-get-terminal-size==1.0.0
  - ipython-genutils==0.2.0
  - prompt-toolkit==1.0.14
@jakirkham
Copy link
Contributor Author

Would be good to hear your thoughts on this one too, @eric-wieser, if you have time.

@eric-wieser
Copy link
Member

I was about to ask about what you just reported in #9479 anyway, so there's obviously something screwy going on here

@eric-wieser
Copy link
Member

Seems to be by design:

static npy_bool
STRING_nonzero (char *ip, PyArrayObject *ap)
{
    int len = PyArray_DESCR(ap)->elsize;
    int i;
    npy_bool nonz = NPY_FALSE;
    npy_bool seen_null = NPY_FALSE;

    for (i = 0; i < len; i++) {
        if (*ip == '\0') {
            seen_null = NPY_TRUE;
        }
        else if (seen_null || !Py_STRING_ISSPACE(*ip)) {
            nonz = NPY_TRUE;
            break;
        }
        ip++;
    }
    return nonz;
}

@eric-wieser
Copy link
Member

See #9848

@jakirkham
Copy link
Contributor Author

Seems you have opened issue ( #9875 ), which more generally captures the problem here. Should we close this out and follow along on that issue instead?

@mattip
Copy link
Member

mattip commented Apr 29, 2019

Duplicate of #9875, please reopen if I misunderstood

@mattip mattip closed this as completed Apr 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants