6

I've found the following puzzling behavior with NumPy and a custom dtype for an ndarray:

import numpy as np

# Make a custom dtype with a single triplet of floats (my actual dtype has other
# components, but this suffices to demonstrate the problem.
dt = np.dtype([('a', np.float64, 3)])

# Make a zero array with this dtype:
points = np.zeros((4, 4), dtype=dt)

# Try to edit an entry:
points[0][0]['a'] = np.array([1, 1, 1])

print points[0][0]['a']

Now, this comes back as containing not [1. 1. 1.] as I would expect, but instead [1. 0. 0.], only performing the assignment on the first coordinate. I can get around this by performing the assignment coordinate-wise, but that seems unnecessary given that the full assignment should certainly be the default behavior in this case.

Any thoughts on what's going on here?

2 Answers 2

3

If you change the ordering of the indices, like this: points['a'][0][0] = np.array([1, 1, 1]), it works ok for me (python 2.6.5, numpy 1.3.0 on Ubuntu 10.04). I wish I knew why.

Sign up to request clarification or add additional context in comments.

2 Comments

Works for me too. Though what I really would have wanted to do would be something like: p = points[0][0] p['a'] = np.array([1, 1, 1]) And other manipulations on p if necessary.
@Tim: AFAIK, it seems natural to first specify the named column, and only then go for the numeric indices. Thus way, ability of writing either points['a'][0] or points[0]['a'] (which works for dtype-ed arrays of PODs) feels like a free lunch to me.
2

There are many method to assign points, if you want your method works:

points[0][0]['a'][:] = np.array([1, 1, 1])

or:

points[0,0]['a'][:] = np.array([1, 1, 1])

because points[0,0]['a'] is an array, if you want to change the content of the array, you shoud use index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.