Skip to content

Commit f8b4fa2

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

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Orange/data/table.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,12 @@ def set_column(self, index: Union[int, str, Variable], data):
17221722
raise ValueError(f"cannot set data for variable {index.name} "
17231723
"with different encoding")
17241724
index = self.domain.index(index)
1725-
self._get_column_view(index)[:] = data
1725+
# Zero-sized arrays cannot be made writeable, yet the below
1726+
# assignment would fail despite doing nothing.
1727+
if len(self) > 0:
1728+
self._get_column_view(index)[:] = data
1729+
else:
1730+
assert len(self) == len(data)
17261731

17271732
def _filter_is_defined(self, columns=None, negate=False):
17281733
# structure of function is obvious; pylint: disable=too-many-branches

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)