8000 can't index string- and unicode- numpy-scalars with an empty tuple · Issue #7267 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

can't index string- and unicode- numpy-scalars with an empty tuple #7267

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

Open
ahaldane opened this issue Feb 17, 2016 · 4 comments
Open

can't index string- and unicode- numpy-scalars with an empty tuple #7267

ahaldane opened this issue Feb 17, 2016 · 4 comments
Labels

Comments

@ahaldane
Copy link
Member

As discovered in #7259 and #7260, string- and unicode- numpy scalars do not support indexing with an empty tuple, unlike other scalars:

>>> numpy.string_("ABC")[()]
TypeError: string indices must be integers, not tuple
>>> numpy.int32(1)[()]
1

This may be a problem because indexing with an empty tuple is a way of converting possible 0-d arrays to scalars:

>>> a = numpy.int64(1)
>>> b = numpy.array(a)
>>> type(a[()]), type(b[()])
(numpy.int64, numpy.int64)

As of writing this it's not 100% clear to me we actually want this. It could be that instead of just writing

return possiblearr[()]

we should encourage people to write something like

if isinstance(possiblearr, ndarray):
    return possiblearr[()]
return possiblearr

But, on the other hand it's probably a good idea for all scalar types to behave similarly. I made a rough outline for a fix in a comment in #7260.

@eric-wieser
Copy link
Member

Related: numpy.string_("ABC")[...] fails in the same way

@charris
Copy link
Member
charris commented Mar 6, 2017

OTOH,

In [5]: numpy.string_("ABC")[1]
Out[5]: 'B'

@eric-wieser
Copy link
Member

This manifests itself in np.ma.array(['ab', 'cd']).take(1) due to the patch in #7586

@eric-wieser
Copy link
Member

Arguably not a bug - we're never going to have this work on object scalars, so having it fail on string scalars too forces the library writer to use a solution that works for object scalars too.

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

No branches or pull requests

3 participants
0