Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'binary': 0.07; 'already.': 0.09; 'arrays': 0.09; 'bits': 0.09; 'rows': 0.09; 'to)': 0.09; 'worked.': 0.09; 'subject:question': 0.10; '(?).': 0.16; '*only*': 0.16; 'numpy': 0.16; 'record,': 0.16; 'sense,': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'tuple': 0.16; 'unpacked': 0.16; 'unpacking)': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'example': 0.22; 'to:name:python-list@python.org': 0.22; 'this?': 0.23; '(or': 0.24; 'question': 0.24; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'to:2**1': 0.27; 'array': 0.29; '(which': 0.31; 'code': 0.31; 'that.': 0.31; 'behaving': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'tuples': 0.31; 'probably': 0.32; 'thanks!': 0.32; '-----': 0.33; 'not.': 0.33; 'could': 0.34; 'subject: (': 0.35; 'december': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; '14,': 0.36; 'shorter': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'email addr:python.org': 0.37; 'clear': 0.37; 'easily': 0.37; 'filled': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'expect': 0.39; 'subject:': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'received:67.195': 0.60; 'received:98.137': 0.60; 'simple,': 0.60; 'new': 0.61; 'show': 0.63; 're:': 0.63; 'email name:python-list': 0.65; 'sample': 0.67; 'header:Reply-To:1': 0.67; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'records': 0.73; 'received:bullet.mail.gq1.yahoo.com': 0.84; 'subject:think': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1418567660; bh=gqqs+VsGGbBVjf63fQRUTMIKHXOZP0qyszj7k2RVrVI=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=I1PYdCVq+UHGl/TQ9ICjtXk3h0nz/fucm/F56fU9Tx3dO5ze5Qgi9eEv6RVEAPUozNhNHSDOm+GcLee0KGOVpS2G5o/iH+V+XgNdQBhbutRkdNPMHf5cvdPJQ3pzs2+bFkam5SA/KPzed4zBriNyTBCYrUvKYs9dtDGlsX9hnlU33fWRlfCEmWFsOmH428FGyW7drVa8+/iICZdbHMUwUQLnaRLUj3RpGQYUF+uDvSyY3Rmj2HzjZ7FMjDo2KBZA4Mxg3yYLzQbuT44wgtcs5XSPvs+IrHuPF1ZwbYN9nzDseOvN9YwbtTb00rzK/Tcv7MW8fH6VRE+BGf8rWfjxug== DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=yahoo.com; b=Xfk2hU70UlWa0DiySP8WzTDrZgfB0Me7zXMww6fx3gui0vh/LRm+Z2sOmGS5lgKTj2Iy+7bT94mdX1Ta2nt7pCrOMqseVMtV7AuPj+SkwYF6fsHGFVBq5ClEqu6ARBNv/3GU380gGZFhzC0JRDlgOi7DjRA5i3jbKzLOJFbGuDrYNRZ253i/jWC1vwAXQIi9mooCB3UBefrlXWuWvJC4SmUXy/w3Zx2OoDhX0W5knlzPFN1Lzb17lMZJ9IDlXq9gdk8RUaiKqnRz/pUZ2l+4EmGOxD5Ops/N1nNwxFNmyd0Nzo2R9dwj9FT2UtEEWJDhJPv+OAJtjSENo9cPL/mgCA==; X-Yahoo-Newman-Property: ymail-4 X-Yahoo-Newman-Id: 73213.92139.bm@omp1005.mail.gq1.yahoo.com X-YMail-OSG: V0TaqSYVM1m9YBh_AlcCI81.wIMNgd6gxa5M2wd7kHXabq4douRiL3C4MfkGU0P CS06Vjg6n.zXCYy2smdnbcwCdGNdoiA.FcO6IJYNKGvFJw7KpsoqUCxL5sMzvG73rlh586MQKsku .CHI4kQJ.lYkfgCvHi_mmmsEKesOZi0dAXrylsMKXIBWRw7BKzUs.V08lDKBEoyOZE5Dpxs5CB0n 3NAKPYMp85bk9g0AUFZwQGsjGtBz93r8Joe7NWpr6ncKInjXGhVw16zjuywTPXfxbPFEnt6zMJwN ddkwDVzVynCnbMbQaFcmL5V.kfdh4mHREfH4uyrTDqkQfvdViJ0WsJcEMNUyMlsEmsCh0WMmnnsY AHEQ3plR7OCV.GK.rCJSD8Khx12Evsokx7xtm13XeVMUk5p4QuT3pNF_nVehipyGP7feYhg8Y.87 vJuBJZGi6aT.WkGJKIp1ZirtFRBkY_X4SkK7ZQ9dbPDhFfvetTOZJWf4IlbtUIJNXR3OTEC4_p0A a8w7mu5Bi4NFSkg-- Date: Sun, 14 Dec 2014 14:31:33 +0000 (UTC) From: Albert-Jan Roskam To: Steven D'Aprano , "python-list@python.org" In-Reply-To: <548cd14c$0$13013$c3e8da3$5496439d@news.astraweb.com> References: <548cd14c$0$13013$c3e8da3$5496439d@news.astraweb.com> Subject: Re: numpy question (fairly basic, I think) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Albert-Jan Roskam List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418567821 news.xs4all.nl 2852 [2001:888:2000:d::a6]:55605 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82503 ----- Original Message ----- > From: Steven D'Aprano > To: python-list@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