Skip to content

Commit 4edd0fb

Browse files
kdorrpalnabarun
authored andcommitted
Integrate _normalize_data with existing infrastructure
1 parent 7197c02 commit 4edd0fb

File tree

3 files changed

+16
-37
lines changed

3 files changed

+16
-37
lines changed

mplaltair/_convert.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import matplotlib.dates as mdates
22
import numpy as np
3-
from ._data import _locate_channel_data, _locate_channel_dtype, _convert_to_mpl_date
3+
from ._data import _locate_channel_data, _locate_channel_dtype, _convert_to_mpl_date, _normalize_data
44

55
def _allowed_ranged_marks(enc_channel, mark):
66
"""TODO: DOCS
@@ -110,6 +110,8 @@ def _convert(chart):
110110
"""
111111
mapping = {}
112112

113+
_normalize_data(chart)
114+
113115
if not chart.to_dict().get('encoding'):
114116
raise ValueError("Encoding not provided with the chart specification")
115117

mplaltair/_data.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88

99
from ._utils import _fetch
1010

11-
def _normalize_data(spec):
11+
def _normalize_data(chart):
1212
"""Converts the data to a Pandas dataframe
1313
1414
Parameters
1515
----------
16-
spec : dict
16+
chart : altair.Chart
1717
The vega-lite specification in json format
1818
1919
Returns
2020
-------
21-
dict
22-
The vega-lite specification with the data format fixed to a Pandas dataframe
21+
None
2322
2423
Raises
2524
------
@@ -30,20 +29,19 @@ def _normalize_data(spec):
3029
Raised when the data specification has an unsupported data source
3130
"""
3231

32+
spec = chart.to_dict()
33+
3334
if not spec.get('data'):
3435
raise ValidationError('Please specify a data source.')
3536

3637
if spec['data'].get('url'):
3738
df = pd.DataFrame(_fetch(spec['data']['url']))
3839
elif spec['data'].get('values'):
39-
df = pd.DataFrame(spec['data']['values'])
40+
return
4041
else:
4142
raise NotImplementedError('Given data specification is unsupported at the moment.')
4243

43-
del spec['data']
44-
spec['data'] = df
45-
46-
return spec
44+
chart.data = df
4745

4846
def _locate_channel_dtype(chart, channel):
4947
"""Locates dtype used for each channel

mplaltair/tests/test_data.py

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,14 @@
1818
})
1919

2020
def test_data_list():
21-
spec = {
22-
"data": {
23-
"values": [{"a": 1, "b": 2}, {"c": 3, "d": 4}]
24-
25-
}
26-
}
27-
assert type(_normalize_data(spec)["data"]) == pd.DataFrame
21+
chart = alt.Chart(pd.DataFrame({'a': [1], 'b': [2], 'c': [3]})).mark_point()
22+
_normalize_data(chart)
23+
assert type(chart.data) == pd.DataFrame
2824

2925
def test_data_url():
30-
spec = {
31-
"data": {
32-
"url": data.cars.url
33-
}
34-
}
35-
assert type(_normalize_data(spec)["data"]) == pd.DataFrame
36-
37-
def test_data_no_pass():
38-
spec = {}
39-
with pytest.raises(ValidationError):
40-
_normalize_data(spec)
41-
42-
def test_data_invalid():
43-
spec = {
44-
"data": {
45-
"source": "path"
46-
}
47-
}
48-
with pytest.raises(NotImplementedError):
49-
_normalize_data(spec)
26+
chart = alt.Chart(data.cars.url).mark_point()
27+
_normalize_data(chart)
28+
assert type(chart.data) == pd.DataFrame
5029

5130
# _locate_channel_data() tests
5231

0 commit comments

Comments
 (0)