Skip to content

Commit

Permalink
fix(vega): allow data item to miss optional field (#2853)
Browse files Browse the repository at this point in the history
* fix(vega): allow data item to miss optional field

The current implementation assume that all items in the dataset has the exact same fields. 
The list of keys are extracted from the first item in the dataset only and will crash if 
* the first item does not include all field names
* some following items miss certain field

* Update panel/pane/vega.py

* fix: typo

meant `k` not `v`

Co-authored-by: Philipp Rudiger <prudiger@anaconda.com>
  • Loading branch information
kristw and philippjfr committed Nov 4, 2021
1 parent 1351e7f commit 8b5e783
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions panel/pane/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def ds_as_cds(dataset):
"""
if len(dataset) == 0:
return {}
data = {k: [] for k, v in dataset[0].items()}
# create a list of unique keys from all items as some items may not include optional fields
keys = sorted(set(k for d in dataset for k in d.keys()))
data = {k: [] for k in keys}
for item in dataset:
for k, v in item.items():
data[k].append(v)
for k in keys:
data[k].append(item.get(k))
data = {k: np.asarray(v) for k, v in data.items()}
return data

Expand Down

0 comments on commit 8b5e783

Please sign in to comment.