Package: altair-transform
altair-transformPython evaluation of Altair/Vega-Lite transforms.altair-transformrequires Python 3.6 or later. Install with:$ pip install altair_transformAltair-transform evaluatesAltairandVega-Litetransforms directly in Python. This can be useful in a number of contexts, illustrated in the examples below.Example: Extracting DataThe Vega-Lite specification includes the ability to apply a
wide range of transformations to input data within the chart
specification. As an example, here is a sliding window average
of a Gaussian random walk, implemented in Altair:importaltairasaltimportnumpyasnpimportpandasaspdrand=np.random.RandomState(12345)df=pd.DataFrame({'x':np.arange(200),'y':rand.randn(200).cumsum()})points=alt.Chart(df).mark_point().encode(x='x:Q',y='y:Q')line=alt.Chart(df).transform_window(ymean='mean(y)',sort=[alt.SortField('x')],frame=[5,5]).mark_line(color='red').encode(x='x:Q',y='ymean:Q')points+lineBecause the transform is encoded within the renderer, however, the
computed values are not directly accessible from the Python layer.This is wherealtair_transformcomes in. It includes a (nearly)
complete Python implementation of Vega-Lite's transform layer, so
that you can easily extract a pandas dataframe with the computed
values shown in the chart:fromaltair_transformimportextract_datadata=extract_data(line)data.head()xyymean00-0.2047080.457749110.2742360.77109322-0.2452031.04132033-0.8009331.336943441.1648471.698085From here, you can work with the transformed data directly
in Python.Example: Pre-Aggregating Large DatasetsAltair creates chart specifications containing the full dataset.
The advantage of this is that the data used to make the chart is entirely transparent; the disadvantage is that it causes issues as datasets grow large.
To prevent users from inadvertently crashing their browsers by trying to send too much data to the frontend, Altair limits the data size by default.
For example, a histogram of 20000 points:importaltairasaltimportpandasaspdimportnumpyasnpnp.random.seed(12345)df=pd.DataFrame({'x':np.random.randn(20000)})chart=alt.Chart(df).mark_bar().encode(alt.X('x',bin=True),y='count()')chartMaxRowsError: The number of rows in your dataset is greater than the maximum allowed (5000). For information on how to plot larger datasets in Altair, see the documentationThere are several possible ways around this, as mentioned in Altair'sFAQ.
Altiar-transform provides another option via thetransform_chart()function, which will pre-transform the data according to the chart specification, so that the final chart specification holds the aggregated data rather than the full dataset:fromaltair_transformimporttransform_chartnew_chart=transform_chart(chart)new_chartExamining the new chart specification, we can see that it contains the pre-aggregated dataset:new_chart.datax_binnedx_binned2count0-4.0-3.0291-3.0-2.04442-2.0-1.027033-1.00.0681540.01.0685851.02.0270662.03.042373.04.022Limitationsaltair_transformcurrently works only for non-compound charts; that is, it cannot transform or extract data from layered, faceted, repeated, or concatenated charts.There are also a number of less-used transform options that are not yet fully supported. These should explicitly raise aNotImplementedErrorif you attempt to use them.