Skip to content
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

ENH: Make dtype iterable #9951

Closed
wants to merge 2 commits into from
Closed

Conversation

eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Nov 2, 2017

Now, iter(dtype) is the same as (d[i] for i in range(len(d))). This adds some consistency:

>>> v = np.array((1, 2, 3), dtype=[('a', int), ('b', float), ('c', str)])[()]   # get a void scalar
>>> v[0]
1
>>> list(v)
[1, 2.0, '']
>>> v.dtype[0]
dtype('int32')
>>> list(v)
TypeError: 'numpy.dtype' object is not iterable
# after this patch
[dtype('int32'), dtype('float64'), dtype('S0')]

Moved from #8814

One perhaps strange consequence:

>>> dt = np.dtype([('a', np.int8), ('b', np.float32, 3)])
>>> np.array(dt)
np.array(np.dtype([('a', 'i1'), ('b', '<f4', (3,))]), dtype=object)
# after this patch
np.array([('a', np.int8), ('b', np.float32, 3)], dtype=object)

Now, iter(dtype) is the same as (d[i] for i in range(len(d)))
@eric-wieser
Copy link
Member Author

Hmm, actually this conflicts with #8615 - perhaps treating structured dtypes as dict-like is better than sequence-like.

@eric-wieser eric-wieser added the 52 - Inactive Pending author response label Nov 22, 2017
Base automatically changed from master to main March 4, 2021 02:04
@charris
Copy link
Member

charris commented Apr 22, 2021

@seberg Thoughts?

@seberg
Copy link
Member

seberg commented Apr 22, 2021

Hmmm, I don't have a clear opinion. I lean slightly towards the ordered dict-like approach (which would mean closing this PR). In the end:

  • I think the dict-like is how we want users to think about structured dtypes (and especially arrays) usually.
  • But: for assignment when casting between different structured dtypes we explicitly use the field-order approach. So for (same-kind, not safe) casts, the order is actually more relevant than the field names.

So whichever choice it probably is a bit weird... There might be a point in refusing the choice, but adding the dict-like methods .keys(), .items(), and maybe .values(). We could add those only to the void dtype now in principle (maybe a bit awkward still, but should not be hard).

@charris charris closed this Apr 6, 2022
@seberg seberg added the 64 - Good Idea Inactive PR with a good start or idea. Consider studying it if you are working on a related issue. label Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 52 - Inactive Pending author response 64 - Good Idea Inactive PR with a good start or idea. Consider studying it if you are working on a related issue. component: numpy._core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants