2

Say I have a numpy structured array (a.k.a. record array):

record_types = np.dtype([
                ('date',object),  #00 - Timestamp
                ('lats',float),   #01 - Latitude
                ('lons',float),   #02 - Longitude
                ('vals',float),   #03 - Value
                ])

data = np.zeros(10, dtype=record_types)

If I try to call the shape attribute, I get (10,)

How can I do something like the following:

y, x = data.shape

To get y = 10 and x = 4

Thanks!

1 Answer 1

4

This is one of the confusing things about structured arrays.

You basically have a (n-1)D array where each item is a C-like struct.

This type of structure allows for all kinds of useful things (easy file IO with binary formats, for example), but it's quite confusing for a lot of other use cases. For what you're doing, you'd likely be better served by using pandas than directly using a structured array.

That having been said, here's how you'd get what you're asking:

def structured_shape(x):
    if len(x.dtype) > 0
        return list(x.shape) + [len(x.dtype)]
    else:
        return x.shape
Sign up to request clarification or add additional context in comments.

2 Comments

I have yet to look into pandas, but this is another great reason. Thank you!
Plus one for recommending pandas while still answering the question.

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.