Created
August 8, 2018 15:54
-
-
Save kdorr/88b9310a57b13ca694077a581198d50f to your computer and use it in GitHub Desktop.
Idea for adding _normalize_data() back.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _normalize_data(chart): | |
"""Converts the data to a Pandas dataframe. Originally Nabarun's code (PR #5). | |
Parameters | |
---------- | |
chart : altair.Chart | |
The Altair chart object | |
""" | |
spec = chart.to_dict() | |
if not spec['data']: | |
raise ValidationError('Please specify a data source.') | |
if spec['data'].get('url'): | |
df = pd.DataFrame(_fetch(spec['data']['url'])) | |
elif spec['data'].get('values'): | |
return # It's already a dataframe | |
else: | |
raise NotImplementedError('Given data specification is unsupported at the moment.') | |
chart.data = df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
import altair as alt | |
import pandas as pd | |
from vega_datasets import data | |
from .._data import _normalize_data | |
from .._exceptions import ValidationError | |
def test_data_list(): | |
chart = alt.Chart(pd.DataFrame({'a': [1], 'b': [2], 'c': [3]})).mark_point() | |
_normalize_data(chart) | |
assert type(chart.data) == pd.DataFrame | |
def test_data_url(): | |
chart = alt.Chart(data.cars.url).mark_point() | |
_normalize_data(chart) | |
assert type(chart.data) == pd.DataFrame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment