Skip to content

Commit c6f94f2

Browse files
committed
Table.__repr__: Fix for sparse data with < 5 instances
Scipy does not support indexing table[:5] if table has less than 5 rows.
1 parent d9d3286 commit c6f94f2

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Orange/data/table.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,11 @@ def __str__(self):
803803
return "[" + ",\n ".join(str(ex) for ex in self)
804804

805805
def __repr__(self):
806-
s = "[" + ",\n ".join(repr(ex) for ex in self[:5])
807-
if len(self) > 5:
806+
head = 5
807+
if self.is_sparse():
808+
head = min(self.X.shape[0], head)
809+
s = "[" + ",\n ".join(repr(ex) for ex in self[:head])
810+
if len(self) > head:
808811
s += ",\n ..."
809812
s += "\n]"
810813
return s

Orange/tests/test_table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,11 @@ def test_is_sparse(self):
12131213
table.X = sp.csr_matrix(table.X)
12141214
self.assertTrue(table.is_sparse())
12151215

1216+
def test_repr_sparse_with_one_row(self):
1217+
table = data.Table("iris")[:1]
1218+
table.X = sp.csr_matrix(table.X)
1219+
repr(table) # make sure repr does not crash
1220+
12161221

12171222
def column_sizes(table):
12181223
return (len(table.domain.attributes),

0 commit comments

Comments
 (0)