[Numpy-discussion] column_stack with mixed data types

Ryan Krauss ryanlists@gmail....
Mon Jul 30 17:22:24 CDT 2007


In writing a function to parse my data, I ran into some unexpected
behavior.  Is it intentional that a recarray can only be created by a
list of tuples and not by a list of lists?

Here is what I ran into:

ipdb>biglist[0:10]
Out[68]:
[[7, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''],
 [8, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''],
 [9, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''],
 [10, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''],
 [11, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''],
 [12, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''],
 [13, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, 'With headliner'],
 [14, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, 'With headliner'],
 [15, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, ''],
 [16, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, '']]
ipdb>tuplelist=[tuple(row) for row in biglist]
ipdb>tuplelist[0:10]
Out[68]:
[(7, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''),
 (8, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''),
 (9, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''),
 (10, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''),
 (11, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''),
 (12, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''),
 (13, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, 'With headliner'),
 (14, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, 'With headliner'),
 (15, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, ''),
 (16, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, '')]
ipdb>dt
Out[68]: dtype([('Test Number', '<i4'), ('Foam Thick. (mm)', '<f8'),
('Foam Description', '|S14'), ('Impactor', '|S14'), ('Mass (kg)',
'|S14'), ('Drop Height (in.)', '<f8'), ('Flag Height (in.)', '<f8'),
('Notes', '|S14')])
ipdb>array(biglist, dtype=dt)
*** TypeError: expected a readable buffer object

This does not work for a list of lists, but does work for a list of tuples:

ipdb>array(tuplelist, dtype=dt)
Out[68]:
array([(7, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''),
       (8, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''),
       (9, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.75, ''),
       (10, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''),
       (11, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''),
       (12, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875, ''),
       (13, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875,
'With headliner'),
       (14, 20.0, 'HE 4.0 pcf', 'Flat', 'Large', 32.6875, 4.6875,
'With headliner'),
       (15, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, ''),
       (16, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, ''),
       (17, 20.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.25, ''),
       (18, 15.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.0, ''),
       (19, 15.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.0, ''),
       (20, 15.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 32.6875, 5.0, ''),
       (21, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.5625, ''),
       (22, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.5625, ''),
       (23, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875, 2.5625, ''),
       (24, 10.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875,
2.2812999999999999, ''),
       (25, 10.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875,
2.2812999999999999, ''),
       (26, 10.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875,
2.2812999999999999, ''),
       (27, 5.0, 'HE 4.0 pcf', 'Flat', 'Small', 32.6875,
2.0937999999999999, ''),
       (28, 20.0, 'P420', 'Flat', 'Small', 32.6875, 2.7812999999999999, ''),
       (29, 20.0, 'P420', 'Flat', 'Small', 32.6875, 2.7812999999999999, ''),
       (30, 20.0, 'E175', 'Flat', 'Small', 32.6875, 2.7812999999999999, ''),
       (31, 20.0, 'E175', 'Flat', 'Small', 32.6875, 2.7812999999999999, ''),
       (32, 20.0, 'E175', 'Flat', 'Small', 32.6875, 2.7812999999999999, ''),
       (33, 20.0, 'E175', 'Flat', 'Small', 32.6875, 2.7812999999999999, ''),
       (34, 15.0, 'E175', 'Flat', 'Small', 32.6875, 2.5, ''),
       (35, 15.0, 'E175', 'Flat', 'Small', 32.6875, 2.5, ''),
       (36, 15.0, 'E175', 'Flat', 'Small', 32.6875, 2.5, ''),
       (37, 20.0, 'E175', 'Hemi', '0.2493', 32.6875, 5.1875, ''),
       (38, 20.0, 'E175', 'Hemi', '0.2493', 32.6875, 5.1875, ''),
       (39, 20.0, 'E175', 'Hemi', '0.2493', 32.6875, 5.1875, ''),
       (40, 15.0, 'E175', 'Hemi', '0.2493', 32.6875, 5.0, ''),
       (41, 15.0, 'E175', 'Hemi', '0.2493', 32.6875, 5.0, ''),
       (42, 15.0, 'E175', 'Hemi', '0.2493', 32.6875, 5.0, ''),
       (43, 15.0, 'E175', 'Flat', 'Small', 10.5625, 2.5625, ''),
       (44, 15.0, 'E175', 'Flat', 'Small', 10.5625, 2.5625, ''),
       (45, 15.0, 'E175', 'Flat', 'Small', 10.5625, 2.5625, ''),
       (46, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.5625, 2.5625, ''),
       (47, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.5625, 2.5625, ''),
       (48, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.5625, 2.5625, ''),
       (49, 10.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.25, 2.25, ''),
       (50, 10.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.25, 2.25, ''),
       (51, 10.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.25, 2.25, ''),
       (52, 10.0, 'E175', 'Flat', 'Small', 10.25, 2.25, ''),
       (53, 10.0, 'E175', 'Flat', 'Small', 10.25, 2.25, ''),
       (54, 10.0, 'E175', 'Flat', 'Small', 10.25, 2.25, ''),
       (55, 5.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.125, 2.1875, ''),
       (56, 5.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.125, 2.1875, ''),
       (57, 5.0, 'HE 4.0 pcf', 'Flat', 'Small', 10.125, 2.1875, ''),
       (58, 15.0, 'HE 4.0 pcf', 'Flat', 'Large', 12.5, 4.5, ''),
       (59, 15.0, 'HE 4.0 pcf', 'Flat', 'Large', 12.5, 4.5, ''),
       (60, 15.0, 'HE 4.0 pcf', 'Flat', 'Large', 12.5, 4.5, ''),
       (61, 15.0, 'E175', 'Flat', 'Large', 12.5, 4.5, ''),
       (62, 15.0, 'E175', 'Flat', 'Large', 12.5, 4.5, ''),
       (63, 15.0, 'E175', 'Flat', 'Large', 12.5, 4.5, ''),
       (64, 10.0, 'HE 4.0 pcf', 'Flat', 'Large', 12.125, 4.375, ''),
       (65, 10.0, 'HE 4.0 pcf', 'Flat', 'Large', 12.125, 4.375, ''),
       (66, 10.0, 'HE 4.0 pcf', 'Flat', 'Large', 12.125, 4.375, ''),
       (67, 10.0, 'E175', 'Flat', 'Large', 12.125, 4.375, ''),
       (68, 10.0, 'E175', 'Flat', 'Large', 12.125, 4.375, ''),
       (69, 10.0, 'E175', 'Flat', 'Large', 12.125, 4.375, ''),
       (70, 10.0, 'E175', 'Hemi', '0.2493', 12.875, 4.8125, ''),
       (71, 10.0, 'E175', 'Hemi', '0.2493', 12.875, 4.8125, ''),
       (72, 10.0, 'E175', 'Hemi', '0.2493', 12.875, 4.8125, ''),
       (73, 10.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 12.875, 4.8125, ''),
       (74, 10.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 12.875, 4.8125, ''),
       (75, 10.0, 'HE 4.0 pcf', 'Hemi', '0.2493', 12.875, 4.8125, ''),
       (76, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 18.3125, 2.5, ''),
       (77, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 18.3125, 2.5, ''),
       (78, 15.0, 'HE 4.0 pcf', 'Flat', 'Small', 18.3125, 2.5, ''),
       (79, 15.0, 'E175', 'Flat', 'Small', 18.3125, 2.5, ''),
       (80, 15.0, 'E175', 'Flat', 'Small', 18.3125, 2.5, ''),
       (81, 15.0, 'E175', 'Flat', 'Small', 18.3125, 2.5, ''),
       (82, 20.0, 'E175', 'Flat', 'Small', 45.6875, 2.7187999999999999, ''),
       (83, 20.0, 'E175', 'Flat', 'Small', 45.6875, 2.7187999999999999, ''),
       (84, 20.0, 'E175', 'Flat', 'Small', 45.6875, 2.7187999999999999, ''),
       (85, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 45.6875,
2.7187999999999999, ''),
       (86, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 45.6875,
2.7187999999999999, ''),
       (87, 20.0, 'HE 4.0 pcf', 'Flat', 'Small', 45.6875,
2.7187999999999999, '')],
      dtype=[('Test Number', '<i4'), ('Foam Thick. (mm)', '<f8'),
('Foam Description', '|S14'), ('Impactor', '|S14'), ('Mass (kg)',
'|S14'), ('Drop Height (in.)', '<f8'), ('Flag Height (in.)', '<f8'),
('Notes', '|S14')])
ipdb>


On 7/30/07, Ryan Krauss <ryanlists@gmail.com> wrote:
> I have data in a spreadsheet where the first column is an integer. the
> second is a float, columns 3-5 are strings, and columns 6 and 7 are
> floats.  I have each column as a list, but when I use column_stack, I
> get back a 2D array of strings.  What is the easiest way to get a
> recarray out of this list of lists?  Is recarray my best/only choice?
>
> Thanks,
>
> Ryan
>


More information about the Numpy-discussion mailing list