Skip to content
Snippets Groups Projects

FIX: `to_table` failed on lists

Merged Daniel Hornung requested to merge f-fix-table-converter-list into dev
All threads resolved!
1 file
+ 4
4
Compare changes
  • Side-by-side
  • Inline
@@ -27,6 +27,7 @@ from tempfile import NamedTemporaryFile
import caosdb as db
import pandas as pd
from caosdb.apiutils import compare_entities
from numpy import nan
from caosadvancedtools.table_converter import (from_table, from_tsv, to_table,
to_tsv)
@@ -42,7 +43,8 @@ class TableTest(unittest.TestCase):
def test_empty(self):
c = db.Container()
self.assertRaises(ValueError, to_table, c)
df = to_table(c)
assert df.shape == (0, 0)
def test_different_props(self):
r1 = db.Record()
@@ -65,6 +67,36 @@ class TableTest(unittest.TestCase):
c.extend([r1, r2])
self.assertRaises(ValueError, to_table, c)
def test_list(self):
r1 = db.Record()
r1.add_parent("no1")
r1.add_property("p1", value=1)
r1.add_property("p3", value=23)
r1.add_property("p4", value=[1])
r2 = db.Record()
r2.add_parent("no1")
r2.add_property("p1")
r2.add_property("p2", value=[20, 21])
r2.add_property("p3", value=[30, 31])
r2.add_property("p4", value=[40.0, 41.0])
r3 = db.Record()
r3.add_parent("no1")
r3.add_property("p5", value=[50, 51])
c = db.Container()
c.extend([r1, r2, r3])
result = to_table(c)
# NaN is hard to compare, so we replace it by -999
# autopep8: off
assert result.replace(to_replace=nan, value=-999).to_dict() == {
'p1': {0: 1, 1: -999, 2: -999}, # noqa: E202
'p3': {0: 23, 1: [30, 31], 2: -999}, # noqa: E202
'p4': {0: [1], 1: [40.0, 41.0], 2: -999}, # noqa: E202
'p2': {0: -999, 1: [20, 21], 2: -999}, # noqa: E202
'p5': {0: -999, 1: -999, 2: [50, 51]}
}
# autopep8: on
assert list(result.dtypes) == [float, object, object, object, object]
class FromTsvTest(unittest.TestCase):
def test_basic(self):
Loading