def extract_data(chart: alt.Chart) -> pd.DataFrame: """Extract transformed data from a chart. This only works with data and transform defined at the top level of the chart. Parameters ---------- chart : alt.Chart The chart instance from which the data and transform will be extracted Returns ------- df_transformed : pd.DataFrame The extracted and transformed dataframe. """ return apply(to_dataframe(chart.data, chart), chart.transform)
def extract_data( chart: alt.Chart, apply_encoding_transforms: bool = True ) -> pd.DataFrame: """Extract transformed data from a chart. This only works with data and transform defined at the top level of the chart. Parameters ---------- chart : alt.Chart The chart instance from which the data and transform will be extracted apply_encoding_transforms : bool If True (default), then apply transforms specified within an encoding as well as those specified directly in the transforms attribute. Returns ------- df_transformed : pd.DataFrame The extracted and transformed dataframe. Example ------- >>> import pandas as pd >>> data = pd.DataFrame({'x': range(5), 'y': list('ABCAB')}) >>> chart = alt.Chart(data).mark_bar().encode(x='sum(x)', y='y') >>> extract_data(chart) y sum_x 0 A 3 1 B 5 2 C 2 """ if apply_encoding_transforms: chart = extract_transform(chart) return apply(to_dataframe(chart.data, chart), chart.transform)
def test_named_to_dataframe(df, chart, named_data, data_type): data = data_type(named_data) assert df.equals(to_dataframe(data, context=chart))
def test_inline_to_dataframe(df, inline_data, data_type): data = data_type(inline_data) assert df.equals(to_dataframe(data))
def test_json_to_dataframe(df, json_data, data_type): data = data_type(json_data) assert df.equals(to_dataframe(data))
def test_csv_to_dataframe(df, csv_data, data_type): data = data_type(csv_data) assert df.equals(to_dataframe(data))
def test_sequence_to_dataframe(df, sequence_data, data_type): data = data_type(sequence_data) assert df[["x"]].equals(to_dataframe(data))