scikit-hep / awkward-0.x

Manipulate arrays of complex data structures as easily as Numpy.
BSD 3-Clause "New" or "Revised" License
215 stars 39 forks source link

Fix iteration over Rows #188

Closed masonproffitt closed 5 years ago

masonproffitt commented 5 years ago

Currently, iterating over a Row in a Table with columns that aren't labeled '0', '1', '2', etc., will fail, making it appear as if every Row is empty:

>>> import awkward
>>> t = awkward.Table({'a': [1]})
<Table [<Row 0>] at 0x7fc15bf93090>
>>> t['a']
array([1])
>>> t[0]
<Row 0>
>>> len(t[0])     
0

This is fixed here, so that the actual OrderedDict of column names will be iterated over.

jpivarski commented 5 years ago

I agree that a Row shouldn't look like an empty iterable. However, it's unpythonic for iteration over a dict-like thing (a Row is dict-like; that's why its .tolist() representation is a dict) to iterate over the values, rather than the keys. I see what your example code is trying to do, but in general, it doesn't satisfy the criterion of least surprise.

Personally, I would have been much happier if iteration over dict-like things in Python was essentially items(), rather than values() (as you have it here) or keys() (as it almost always is). However, we can't break pythonic conventions.

masonproffitt commented 5 years ago

Okay, I think it seems correct now.

jpivarski commented 5 years ago

Sorry that I missed the fact that this is done. I just checked the tests, and they are requiring the right behavior (and pass).