Skip to content

Commit 7d2e9ff

Browse files
authored
Merge pull request #5413 from PrimozGodec/speedup-table-to-frame
[FIX] Speed-up slow table_to_frame
2 parents 98af325 + f92b98e commit 7d2e9ff

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Orange/data/pandas_compat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ def _column_to_series(col, vals):
368368
elif col.is_continuous:
369369
dt = float
370370
# np.nan are not compatible with int column
371-
nan_values_in_column = [t for t in vals if np.isnan(t)]
372-
if col.number_of_decimals == 0 and len(nan_values_in_column) == 0:
371+
if col.number_of_decimals == 0 and not np.any(np.isnan(vals)):
373372
dt = int
374373
result = (col.name, pd.Series(vals).astype(dt))
375374
elif col.is_string:

Orange/data/tests/test_pandas.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@ def test_table_to_frame(self):
8181
self.assertEqual(list(df['sepal length'])[0:4], [5.1, 4.9, 4.7, 4.6])
8282
self.assertEqual(list(df['iris'])[0:2], ['Iris-setosa', 'Iris-setosa'])
8383

84+
def test_table_to_frame_nans(self):
85+
from Orange.data.pandas_compat import table_to_frame
86+
domain = Domain(
87+
[ContinuousVariable("a", number_of_decimals=0), ContinuousVariable("b")]
88+
)
89+
table = Table(
90+
domain, np.column_stack((np.ones(10), np.hstack((np.ones(9), [np.nan]))))
91+
)
92+
93+
df = table_to_frame(table)
94+
table_column_names = [var.name for var in table.domain.variables]
95+
frame_column_names = df.columns
96+
97+
self.assertEqual(sorted(table_column_names), sorted(frame_column_names))
98+
self.assertEqual(df["a"].dtype, int)
99+
self.assertEqual(df["b"].dtype, float)
100+
self.assertEqual([1, 1, 1], list(df["a"].iloc[-3:]))
101+
self.assertTrue(np.isnan(df["b"].iloc[-1]))
102+
84103
def test_table_to_frame_metas(self):
85104
from Orange.data.pandas_compat import table_to_frame
86105

0 commit comments

Comments
 (0)