numpy question (fairly basic, I think)

Albert-Jan Roskam fomcl at yahoo.com
Sun Dec 14 09:31:33 EST 2014



----- Original Message -----

> From: Steven D'Aprano <steve+comp.lang.python at pearwood.info>
> To: python-list at python.org
> Cc: 
> Sent: Sunday, December 14, 2014 12:52 AM
> Subject: Re: numpy question (fairly basic, I think)
> 
> Albert-Jan Roskam wrote:
> 
>>  Hi,
>> 
>>  I am new to numpy. I am reading binary data one record at a time (I have
>>  to) and I would like to store all the records in a numpy array which I
>>  pre-allocate. Below I try to fill the empty array with exactly one record,
>>  but it is filled with as many rows as there are columns. Why is this? It
>>  is probably something simple, but I am stuck! It is like the original
>>  record is not unpacked *as in tuple unpacking) into the array, so it
>>  remains one chunk, not an (nrows, ncols) structure.
> 
> Can you simplify the example to something shorter that focuses on the issue
> at hand? It isn't clear to me which bits of the code you show are behaving
> the way you expect and which bits are not.


Hi Steven,

Thanks for replying. My code was so elaborate because I did not know which part made it go wrong. I think I have got it already. Numpy arrays (ndarrays) must be homogeneous wrt their datatype (dtype). 

Probably to make vectorization work (?). However, a structured array (which I was using) *can* contain multiple dtypes, but it can only be one-dimensional. Its records are tuples (or arrays). In this sense, even a structured array is homogeneous. I was trying to change the one-dim array into a two-dim array so I could easily retrieve columns. I now use a pandas DataFrame to do that. If my sample data would have contained *only* floats (or ints, or ...), my original approach would have worked. 


Thanks!

Albert-Jan



More information about the Python-list mailing list