Skip to content

Commit 87142fa

Browse files
committed
Adds data normalizer to _data.py
1 parent a4f7dbc commit 87142fa

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
@@ -4,6 +4,47 @@
44
from datetime import datetime
55
import numpy as np
66

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

0 commit comments

Comments
 (0)