Skip to content

Commit 23357e0

Browse files
committed
Adds data normalizer to _data.py
1 parent 7a6f383 commit 23357e0

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

mplaltair/_data.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
from ._exceptions import ValidationError
22

3+
import pandas as pd
4+
5+
from ._utils import _fetch
6+
7+
def _normalize_data(spec):
8+
"""Converts the data to a Pandas dataframe
9+
10+
Parameters
11+
----------
12+
spec : dict
13+
The vega-lite specification in json format
14+
15+
Returns
16+
-------
17+
dict
18+
The vega-lite specification with the data format fixed to a Pandas dataframe
19+
20+
Raises
21+
------
22+
ValidationError
23+
Raised when the specification does not contain any data attribute
24+
25+
NotImplementedError
26+
Raised when the data specification has an unsupported data source
27+
"""
28+
29+
if not spec.get('data'):
30+
raise ValidationError('Please specify a data source.')
31+
32+
if spec['data'].get('url'):
33+
df = pd.DataFrame(_fetch(spec['data']['url']))
34+
elif spec['data'].get('values'):
35+
df = pd.DataFrame(spec['data']['values'])
36+
else:
37+
raise NotImplementedError('Given data specification is unsupported at the moment.')
38+
39+
del spec['data']
40+
spec['data'] = df
41+
42+
return spec
43+
344
def _locate_channel_dtype(chart, channel):
445
"""Locates dtype used for each channel
546
Parameters

0 commit comments

Comments
 (0)