Skip to content

Commit b81907b

Browse files
committed
table: Ensure correct dtype in _compute_distributions
Fix an 'ValueError: cannot convert float NaN to integer' in bincount when the column data comes from a object array and contains NaN values.
1 parent 53b0321 commit b81907b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Orange/data/table.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,8 @@ def _get_matrix(M, cachedM, col):
13091309
m, W, Xcsc = _get_matrix(self.X, Xcsc, col)
13101310
elif col < 0:
13111311
m, W, Xcsc = _get_matrix(self.metas, Xcsc, col * (-1) - 1)
1312+
if np.issubdtype(m.dtype, np.dtype(object)):
1313+
m = m.astype(float)
13121314
else:
13131315
m, W, Ycsc = _get_matrix(self._Y, Ycsc, col - self.X.shape[1])
13141316
if var.is_discrete:

Orange/tests/test_distribution.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,13 @@ def test_compute_distributions_metas(self):
430430
variable = d.domain[-2]
431431
dist, _ = d._compute_distributions([variable])[0]
432432
np.testing.assert_almost_equal(dist, [3, 3, 2])
433-
433+
# repeat with nan values
434+
assert d.metas.dtype.kind == "O"
435+
assert d.metas[0, 1] == 0
436+
d.metas[0, 1] = np.nan
437+
dist, nanc = d._compute_distributions([variable])[0]
438+
np.testing.assert_almost_equal(dist, [2, 3, 2])
439+
self.assertEqual(nanc, 1)
434440

435441
if __name__ == "__main__":
436442
unittest.main()

0 commit comments

Comments
 (0)