Skip to content
Snippets Groups Projects
Verified Commit c3c6dfa9 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

FIX: Simplified the code a lot again.

parent 7b968097
No related branches found
No related tags found
2 merge requests!100WIP: Filling XLSX: Seems to be working.,!99FIX: `to_table` failed on lists
Pipeline #50261 passed with warnings
......@@ -49,32 +49,26 @@ def generate_property_name(prop):
def to_table(container):
""" creates a table from the records in a container """
"""Create a table from the records in a container."""
if len(container) == 0:
raise ValueError("Container is empty")
properties = set()
for rec in container:
properties.update([generate_property_name(p)
for p in container[0].get_properties()])
df = pd.DataFrame(columns=list(properties))
# TODO Why not this? return pd.DataFrame()
rts = {p.name for p in container[0].parents}
for ii, rec in enumerate(container):
data = []
for rec in container:
if {p.name for p in rec.parents} != rts:
raise ValueError("Parents differ")
row_dict = {}
for prop in rec.get_properties():
propname = generate_property_name(prop)
if isinstance(prop.value, list):
if propname not in df:
df[propname] = pd.Series(dtype=object)
elif df[propname].dtype != object:
df[propname] = df[propname].astype(object)
df.at[ii, propname] = prop.value
return df
row_dict[propname] = prop.value
data.append(row_dict)
result = pd.DataFrame(data=data)
return result
def from_table(spreadsheet, recordtype):
......
......@@ -77,8 +77,11 @@ class TableTest(unittest.TestCase):
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])
c.extend([r1, r2, r3])
to_table(c)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment