Skip to content

Commit eefca74

Browse files
committed
Fix exxeleron#17: corrected handling of nested lists of homogenous length
1 parent 3a5eebf commit eefca74

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

qpython/_pandas.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,11 @@ def _read_table(self, qtype = QTABLE, options = READER_CONFIGURATION):
7878
meta[columns[i]] = QSTRING
7979
odict[columns[i]] = numpy.array(list(data[i]), dtype = numpy.str)
8080
elif isinstance(data[i], (list, tuple)):
81-
# convert character list (represented as string) to numpy representation
8281
meta[columns[i]] = QGENERAL_LIST
83-
odict[columns[i]] = numpy.array(list(data[i]))
82+
tarray = numpy.ndarray(shape = len(data[i]), dtype = numpy.dtype('O'))
83+
for j in xrange(len(data[i])):
84+
tarray[j] = data[i][j]
85+
odict[columns[i]] = tarray
8486
else:
8587
meta[columns[i]] = data[i].meta.qtype
8688
odict[columns[i]] = data[i]

qpython/qcollection.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ def qlist(array, adjust_dtype = True, **meta):
165165
:raises: `ValueError`
166166
'''
167167
if type(array) in (list, tuple):
168-
array = numpy.array(array)
168+
if meta and 'qtype' in meta and meta['qtype'] == QGENERAL_LIST:
169+
# force shape and dtype for generic lists
170+
tarray = numpy.ndarray(shape = len(array), dtype = numpy.dtype('O'))
171+
for i in xrange(len(array)):
172+
tarray[i] = array[i]
173+
array = tarray
174+
else:
175+
array = numpy.array(array)
169176

170177
if not isinstance(array, numpy.ndarray):
171178
raise ValueError('array parameter is expected to be of type: numpy.ndarray, list or tuple. Was: %s' % type(array))
@@ -366,8 +373,11 @@ def qtable(columns, data, **meta):
366373
if columns[i] in meta:
367374
data[i] = qlist(data[i], qtype = meta[columns[i]])
368375
elif not isinstance(data[i], QList):
369-
data[i] = qlist(data[i])
370-
376+
if type(data[i]) in (list, tuple):
377+
data[i] = qlist(data[i], qtype = QGENERAL_LIST)
378+
else:
379+
data[i] = qlist(data[i])
380+
371381
meta[columns[i]] = data[i].meta.qtype
372382
dtypes.append((columns[i], data[i].dtype))
373383

tests/QExpressions3.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ flip `name`iq`misc!(`Dent`Beeblebrox`Prefect;98 42 126;("The Hitch Hiker's Guide
200200
6200630B00030000006E616D65006971006D697363000000030000000B000300000044656E7400426565626C6562726F7800507265666563740007000300000062000000000000002A000000000000007E000000000000000000030000000A00250000005468652048697463682048696B6572277320477569646520746F207468652047616C617879F9A000000000000000F226E3FFFF
201201
([] sc:1 2 3; nsc:(1 2; 3 4; 5 6 7))
202202
6200630B00020000007363006E7363000000020000000700030000000100000000000000020000000000000003000000000000000000030000000700020000000100000000000000020000000000000007000200000003000000000000000400000000000000070003000000050000000000000006000000000000000700000000000000
203+
([] sc:1 2 3; nsc:(1 2; 3 4; 5 6))
204+
6200630B00020000007363006E736300000002000000070003000000010000000000000002000000000000000300000000000000000003000000070002000000010000000000000002000000000000000700020000000300000000000000040000000000000007000200000005000000000000000600000000000000
203205
([] name:`symbol$(); iq:`int$())
204206
6200630B00020000006E616D65006971000000020000000B0000000000060000000000
205207
([] pos:`d1`d2`d3;dates:(2001.01.01;2000.05.01;0Nd))

tests/pandas_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
125125
('nsc', [pandas.Series(numpy.array([1, 2], dtype = numpy.int64)), pandas.Series(numpy.array([3, 4], dtype = numpy.int64)), pandas.Series(numpy.array([5, 6, 7], dtype = numpy.int64))])))
126126
),
127127
'meta': MetaData(**{'qtype': QTABLE, 'nsc': QGENERAL_LIST, 'sc': QLONG_LIST}) }),
128+
('([] sc:1 2 3; nsc:(1 2; 3 4; 5 6))', {'data': pandas.DataFrame(OrderedDict((('sc', pandas.Series(numpy.array([1, 2, 3], dtype = numpy.int64))),
129+
('nsc', [pandas.Series(numpy.array([1, 2], dtype = numpy.int64)), pandas.Series(numpy.array([3, 4], dtype = numpy.int64)), pandas.Series(numpy.array([5, 6], dtype = numpy.int64))])))
130+
),
131+
'meta': MetaData(**{'qtype': QTABLE, 'nsc': QGENERAL_LIST, 'sc': QLONG_LIST}) }),
128132
('([] name:`symbol$(); iq:`int$())', {'data': pandas.DataFrame(OrderedDict((('name', pandas.Series(numpy.array([], dtype = numpy.string_))),
129133
('iq', pandas.Series(numpy.array([], dtype = numpy.int32)))))
130134
),

tests/qreader_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@
154154
[qlist(numpy.array([1, 2]), qtype = QLONG_LIST),
155155
qlist(numpy.array([3, 4]), qtype = QLONG_LIST),
156156
qlist(numpy.array([5, 6, 7]), qtype = QLONG_LIST)]])),
157+
('([] sc:1 2 3; nsc:(1 2; 3 4; 5 6))', qtable(qlist(numpy.array(['sc', 'nsc']), qtype = QSYMBOL_LIST),
158+
[qlist(numpy.array([1, 2, 3]), qtype = QLONG_LIST),
159+
[qlist(numpy.array([1, 2]), qtype = QLONG_LIST),
160+
qlist(numpy.array([3, 4]), qtype = QLONG_LIST),
161+
qlist(numpy.array([5, 6]), qtype = QLONG_LIST)]])),
157162
('([] name:`symbol$(); iq:`int$())', qtable(qlist(numpy.array(['name', 'iq']), qtype = QSYMBOL_LIST),
158163
[qlist(numpy.array([], dtype=numpy.string_), qtype = QSYMBOL_LIST),
159164
qlist(numpy.array([]), qtype = QINT_LIST)])),

tests/qwriter_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@
289289
[qlist([1, 2], qtype = QLONG_LIST),
290290
qlist([3, 4], qtype = QLONG_LIST),
291291
qlist([5, 6, 7], qtype = QLONG_LIST)]]))),
292+
('([] sc:1 2 3; nsc:(1 2; 3 4; 5 6))', qtable(qlist(numpy.array(['sc', 'nsc']), qtype = QSYMBOL_LIST),
293+
[qlist(numpy.array([1, 2, 3]), qtype = QLONG_LIST),
294+
[qlist(numpy.array([1, 2]), qtype = QLONG_LIST),
295+
qlist(numpy.array([3, 4]), qtype = QLONG_LIST),
296+
qlist(numpy.array([5, 6]), qtype = QLONG_LIST)]])),
292297
('([] name:`symbol$(); iq:`int$())', (qtable(qlist(numpy.array(['name', 'iq']), qtype = QSYMBOL_LIST),
293298
[qlist(numpy.array([], dtype=numpy.string_), qtype = QSYMBOL_LIST),
294299
qlist(numpy.array([], dtype=numpy.int32), qtype = QINT_LIST)]),

0 commit comments

Comments
 (0)