Skip to content

structured arrays should raise Exception if missing fields are requested in the form of a list #5262

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
gerritholl opened this issue Nov 5, 2014 · 2 comments

Comments

@gerritholl
Copy link
Contributor

If we have an ndarray with a structured dtype, and I index it with a single non-existent field, an exception (ValueError) is raised, as expected. However, if I index it with a list of fields, of which at least one is non-existent, no such exception is raised:

In [402]: A = numpy.empty(shape=(5,), dtype=[("A", "f2"), ("B", "f4"), ("C", "f8")])

In [403]: A["D"] # expect: ValueError
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-403-6027a6da83aa> in <module>()
----> 1 A["D"] # expect: ValueError

ValueError: field named D not found

In [404]: A[["C", "D"]] # expect: ValueError
Out[404]: 
array([(3.822678085115078e-297,), (-3.1341142554817164e-294,),
       (3.8820982892244793e-265,), (1.9848357676925587e-263,),
       (1.2046357989657346e-307,)], 
      dtype=[('C', '<f8')])

In [405]: A[["D"]] # expect: ValueError
Out[405]: 
array([(), (), (), (), ()], 
      dtype='{'names':[], 'formats':[], 'offsets':[], 'itemsize':14}')

I think it would be more consistent if an error would be raised in both cases.

@njsmith
Copy link
Member

njsmith commented Nov 6, 2014

Sounds correct to me. There's some chance that someone somewhere is
intentionally indexing with missing fields, and if such a person shows up
we might have to go through a deprecation cycle before making this a hard
error. I'm inclined to ignore this possibility until such a person actually
does show up, though, because if you ask for 3 fields and get 2 then you
are going to have problems in most cases already; this behavior seems
pretty difficult to (mis)use correctly.

Want to have a go at putting together a pull request?
On 5 Nov 2014 23:36, "gerritholl" notifications@github.com wrote:

If we have an ndarray with a structured dtype, and I index it with a
single non-existent field, an exception (ValueError) is raised, as
expected. However, if I index it with a list of fields, of which at least
one is non-existent, no such exception is raised:

In [402]: A = numpy.empty(shape=(5,), dtype=[("A", "f2"), ("B", "f4"), ("C", "f8")])

In [403]: A["D"] # expect: ValueError

ValueError Traceback (most recent call last)
in ()
----> 1 A["D"] # expect: ValueError

ValueError: field named D not found

In [404]: A[["C", "D"]] # expect: ValueError
Out[404]:
array([(3.822678085115078e-297,), (-3.1341142554817164e-294,),
(3.8820982892244793e-265,), (1.9848357676925587e-263,),
(1.2046357989657346e-307,)],
dtype=[('C', '<f8')])

In [405]: A[["D"]] # expect: ValueError
Out[405]:
array([(), (), (), (), ()],
dtype='{'names':[], 'formats':[], 'offsets':[], 'itemsize':14}')

I think it would be more consistent if an error would be raised in both
cases.


Reply to this email directly or view it on GitHub
#5262.

@ahaldane
Copy link
Member

Fixed by #5636

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

4 participants