Skip to content

Commit 14d74d0

Browse files
authored
Merge pull request #4963 from markotoplak/fix-from-table-slicing
[FIX] Fix slicing in from_table
2 parents 9f25898 + 9bcc22a commit 14d74d0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

Orange/data/table.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,7 @@ def get_columns(row_indices, src_cols, n_rows, dtype=np.float64,
446446
return table
447447

448448
if isinstance(row_indices, slice):
449-
start, stop, stride = row_indices.indices(source.X.shape[0])
450-
n_rows = (stop - start) // stride
451-
if n_rows < 0:
452-
n_rows = 0
449+
n_rows = len(range(*row_indices.indices(source.X.shape[0])))
453450
elif row_indices is ...:
454451
n_rows = len(source)
455452
else:

Orange/tests/test_table.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,11 +1138,6 @@ class TableTests(unittest.TestCase):
11381138
nrows = 10
11391139
row_indices = (1, 5, 7, 9)
11401140

1141-
data = np.random.random((nrows, len(attributes)))
1142-
class_data = np.random.random((nrows, len(class_vars)))
1143-
meta_data = np.random.random((nrows, len(metas)))
1144-
weight_data = np.random.random((nrows, 1))
1145-
11461141
def setUp(self):
11471142
self.data = np.random.random((self.nrows, len(self.attributes)))
11481143
self.class_data = np.random.random((self.nrows, len(self.class_vars)))
@@ -1584,11 +1579,13 @@ class CreateTableWithDomainAndTable(TableTests):
15841579
slice(None), # [:] - all elements
15851580
slice(None, None, 2), # [::2] - even elements
15861581
slice(None, None, -1), # [::-1]- all elements reversed
1582+
slice(9, 5, -10), # slice a big negative stride and thus 1 element
15871583
]
15881584

15891585
row_indices = [1, 5, 6, 7]
15901586

15911587
def setUp(self):
1588+
super().setUp()
15921589
self.domain = self.create_domain(
15931590
self.attributes, self.class_vars, self.metas)
15941591
self.table = data.Table(
@@ -1632,12 +1629,27 @@ def test_can_filter_rows_with_list(self):
16321629
self.assert_table_with_filter_matches(
16331630
new_table, self.table, rows=indices)
16341631

1635-
def test_can_filter_row_with_slice(self):
1632+
@patch.object(Table, "from_table_rows", wraps=Table.from_table_rows)
1633+
def test_can_filter_row_with_slice_from_table_rows(self, from_table_rows):
1634+
# calling from_table with the same domain will forward to from_table_rows
16361635
for slice_ in self.interesting_slices:
1636+
from_table_rows.reset_mock()
16371637
new_table = data.Table.from_table(
16381638
self.domain, self.table, row_indices=slice_)
16391639
self.assert_table_with_filter_matches(
16401640
new_table, self.table, rows=slice_)
1641+
from_table_rows.assert_called()
1642+
1643+
@patch.object(Table, "from_table_rows", wraps=Table.from_table_rows)
1644+
def test_can_filter_row_with_slice_from_table(self, from_table_rows):
1645+
# calling from_table with a domain copy will use indexing in from_table
1646+
for slice_ in self.interesting_slices:
1647+
from_table_rows.reset_mock()
1648+
new_table = data.Table.from_table(
1649+
self.domain.copy(), self.table, row_indices=slice_)
1650+
self.assert_table_with_filter_matches(
1651+
new_table, self.table, rows=slice_)
1652+
from_table_rows.assert_not_called()
16411653

16421654
def test_can_use_attributes_as_new_columns(self):
16431655
a, _, _ = column_sizes(self.table)

0 commit comments

Comments
 (0)