def test_chart_data_types(): def Chart(data): return alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q") # Url Data data = "/path/to/my/data.csv" dct = Chart(data).to_dict() assert dct["data"] == {"url": data} # Dict Data data = {"values": [{"x": 1, "y": 2}, {"x": 2, "y": 3}]} with alt.data_transformers.enable(consolidate_datasets=False): dct = Chart(data).to_dict() assert dct["data"] == data with alt.data_transformers.enable(consolidate_datasets=True): dct = Chart(data).to_dict() name = dct["data"]["name"] assert dct["datasets"][name] == data["values"] # DataFrame data data = pd.DataFrame({"x": range(5), "y": range(5)}) with alt.data_transformers.enable(consolidate_datasets=False): dct = Chart(data).to_dict() assert dct["data"]["values"] == data.to_dict(orient="records") with alt.data_transformers.enable(consolidate_datasets=True): dct = Chart(data).to_dict() name = dct["data"]["name"] assert dct["datasets"][name] == data.to_dict(orient="records") # Named data object data = alt.NamedData(name="Foo") dct = Chart(data).to_dict() assert dct["data"] == {"name": "Foo"}
def test_chart_data_types(): Chart = lambda data: alt.Chart(data).mark_point().encode(x='x:Q', y='y:Q') # Url Data data = '/path/to/my/data.csv' dct = Chart(data).to_dict() assert dct['data'] == {'url': data} # Dict Data data = {"values": [{"x": 1, "y": 2}, {"x": 2, "y": 3}]} with alt.data_transformers.enable(consolidate_datasets=False): dct = Chart(data).to_dict() assert dct['data'] == data with alt.data_transformers.enable(consolidate_datasets=True): dct = Chart(data).to_dict() name = dct['data']['name'] assert dct['datasets'][name] == data['values'] # DataFrame data data = pd.DataFrame({"x": range(5), "y": range(5)}) with alt.data_transformers.enable(consolidate_datasets=False): dct = Chart(data).to_dict() assert dct['data']['values'] == data.to_dict(orient='records') with alt.data_transformers.enable(consolidate_datasets=True): dct = Chart(data).to_dict() name = dct['data']['name'] assert dct['datasets'][name] == data.to_dict(orient='records') # Named data object data = alt.NamedData(name='Foo') dct = Chart(data).to_dict() assert dct['data'] == {'name': 'Foo'}