Skip to content

Commit

Permalink
Merge pull request #6743 from PrimozGodec/fix-add-column
Browse files Browse the repository at this point in the history
[FIX] Table.add_column - Do not try to insert data in locked empty array
  • Loading branch information
janezd committed Mar 1, 2024
2 parents 0487f58 + f8b4fa2 commit 6c99b83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Orange/data/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,12 @@ def set_column(self, index: Union[int, str, Variable], data):
raise ValueError(f"cannot set data for variable {index.name} "
"with different encoding")
index = self.domain.index(index)
self._get_column_view(index)[:] = data
# Zero-sized arrays cannot be made writeable, yet the below
# assignment would fail despite doing nothing.
if len(self) > 0:
self._get_column_view(index)[:] = data
else:
assert len(self) == len(data)

def _filter_is_defined(self, columns=None, negate=False):
# structure of function is obvious; pylint: disable=too-many-branches
Expand Down
16 changes: 16 additions & 0 deletions Orange/data/tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ def test_with_column(self):
tabw.metas,
np.hstack((tab.metas, np.array(list("abcde")).reshape(5, -1))))

def test_add_column_empty(self):
a, b = ContinuousVariable("a"), ContinuousVariable("b")
table = Table.from_list(Domain([a]), [])

new_table = table.add_column(b, [], to_metas=True)
self.assertTupleEqual(new_table.domain.attributes, (a,))
self.assertTupleEqual(new_table.domain.metas, (b,))
self.assertTupleEqual((0, 1), new_table.X.shape)
self.assertTupleEqual((0, 1), new_table.metas.shape)

new_table = table.add_column(ContinuousVariable("b"), [], to_metas=False)
self.assertTupleEqual(new_table.domain.attributes, (a, b))
self.assertTupleEqual(new_table.domain.metas, ())
self.assertTupleEqual((0, 2), new_table.X.shape)
self.assertTupleEqual((0, 0), new_table.metas.shape)

def test_copy(self):
domain = Domain([ContinuousVariable("x")],
ContinuousVariable("y"),
Expand Down

0 comments on commit 6c99b83

Please sign in to comment.