Skip to content

Commit

Permalink
table_from_frames: fix for non-orange dataframes
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Oct 19, 2021
1 parent a721906 commit 73c31c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Orange/data/pandas_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,12 @@ def table_from_frames(xdf, ydf, mdf):
W = None
for df in dfs:
if isinstance(df, OrangeDataFrame):
W = [df.orange_weights[i] for i in df.index
if i in df.orange_weights]
W = [df.orange_weights[i] for i in df.index if i in df.orange_weights]
if len(W) != len(df.index):
W = None
attributes.update(df.orange_attributes)
else:
W = None
attributes.update(df.orange_attributes)

return Table.from_numpy(
domain,
Expand Down
16 changes: 15 additions & 1 deletion Orange/data/tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def _get_orange_demo_datasets():
self.assertEqual(len(df.columns), len(table.domain.variables), assert_message)

def test_table_from_frames(self):
table = Table("brown-selected")
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()
Expand All @@ -443,6 +443,20 @@ def test_table_from_frames(self):
self.assertTupleEqual(table.domain.metas, new_table.domain.metas)
self.assertEqual(table.domain.class_var, new_table.domain.class_var)

def test_table_from_frames_not_orangedataframe(self):
df = pd.DataFrame(np.random.rand(10, 2), columns=["A", "B"])
df_metas = pd.DataFrame(np.random.rand(10, 2), columns=["C", "D"])
df_y = pd.DataFrame(np.random.rand(10, 1), columns=["Y"])

new_table = Table.from_pandas_dfs(df, df_y, df_metas)

np.testing.assert_array_equal(df, new_table.X)
np.testing.assert_array_equal(df_y, new_table.Y)
np.testing.assert_array_equal(df_metas, new_table.metas)
self.assertListEqual(df.columns, [v.name for v in 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 73c31c2

Please sign in to comment.