Skip to content

Commit

Permalink
Merge pull request biolab#5620 from PrimozGodec/fix-table_from_frames
Browse files Browse the repository at this point in the history
table_from_frames: fix indices parsing
  • Loading branch information
VesnaT authored and markotoplak committed Oct 27, 2021
1 parent 5e58c4e commit 4b03401
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Orange/data/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,13 @@ def table_from_frames(xdf, ydf, mdf):
XYM = (xXYM[0], yXYM[1], mXYM[2])
domain = Domain(xDomain.attributes, yDomain.class_vars, mDomain.metas)

index_iter = (filter(lambda ind: ind.startswith('_o'),
set(df.index[i] for df in dfs))
for i in range(len(xdf.shape[0])))
ids = (i[0] if len(i) == 1 else Table.new_id()
for i in index_iter)
indexes = [df.index for df in dfs]
ids = [
int(x[2:])
if str(x).startswith("_o") and x[2:].isdigit() and x == y == m
else Table.new_id()
for x, y, m in zip(*indexes)
]

attributes = {}
W = None
Expand Down
15 changes: 15 additions & 0 deletions Orange/data/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,21 @@ def _get_orange_demo_datasets():
self.assertEqual(len(df), len(table), assert_message)
self.assertEqual(len(df.columns), len(table.domain.variables), assert_message)

def test_table_from_frames(self):
table = Table("brown-selected") # dataset with all X, Y and metas
table.ids = np.arange(100, len(table) + 100, 1, dtype=int)

x, y, m = table.to_pandas_dfs()
new_table = Table.from_pandas_dfs(x, y, m)

np.testing.assert_array_equal(table.X, new_table.X)
np.testing.assert_array_equal(table.Y, new_table.Y)
np.testing.assert_array_equal(table.metas, new_table.metas)
np.testing.assert_array_equal(table.ids, new_table.ids)
self.assertTupleEqual(table.domain.attributes, new_table.domain.attributes)
self.assertTupleEqual(table.domain.metas, new_table.domain.metas)
self.assertEqual(table.domain.class_var, new_table.domain.class_var)


class TestTablePandas(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 4b03401

Please sign in to comment.