Skip to content

Commit 835e64f

Browse files
committed
Table.add_column - Do not try to insert in locked empty array
1 parent 53710be commit 835e64f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Orange/data/table.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,8 +1452,10 @@ def add_column(self, variable, data, to_metas=None):
14521452
attrs += (variable, )
14531453
domain = Domain(attrs, classes, metavars)
14541454
new_table = self.transform(domain)
1455-
with new_table.unlocked(new_table.metas if to_metas else new_table.X):
1456-
new_table.set_column(variable, data)
1455+
if len(new_table) > 0:
1456+
# empty array doesn't unlock - insert only if array not empty
1457+
with new_table.unlocked(new_table.metas if to_metas else new_table.X):
1458+
new_table.set_column(variable, data)
14571459
return new_table
14581460

14591461
def is_sparse(self):

Orange/data/tests/test_table.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,22 @@ def test_with_column(self):
263263
tabw.metas,
264264
np.hstack((tab.metas, np.array(list("abcde")).reshape(5, -1))))
265265

266+
def test_add_column_empty(self):
267+
a, b = ContinuousVariable("a"), ContinuousVariable("b")
268+
table = Table.from_list(Domain([a]), [])
269+
270+
new_table = table.add_column(b, [], to_metas=True)
271+
self.assertTupleEqual(new_table.domain.attributes, (a,))
272+
self.assertTupleEqual(new_table.domain.metas, (b,))
273+
self.assertTupleEqual((0, 1), new_table.X.shape)
274+
self.assertTupleEqual((0, 1), new_table.metas.shape)
275+
276+
new_table = table.add_column(ContinuousVariable("b"), [], to_metas=False)
277+
self.assertTupleEqual(new_table.domain.attributes, (a, b))
278+
self.assertTupleEqual(new_table.domain.metas, ())
279+
self.assertTupleEqual((0, 2), new_table.X.shape)
280+
self.assertTupleEqual((0, 0), new_table.metas.shape)
281+
266282
def test_copy(self):
267283
domain = Domain([ContinuousVariable("x")],
268284
ContinuousVariable("y"),

0 commit comments

Comments
 (0)