Altair Charts containing Encoding of Special Characters

Question:

I am trying to plot a chart from a spreadsheet with this code.

import pandas as pd
import altair as alt
crude_df = pd.read_excel(open('PET_CONS_PSUP_DC_NUS_MBBLPD_M.xls', 'rb'),
              sheet_name='Data 1',index_col=None, header=2)  

alt.Chart(crude_df.tail(100)).mark_circle().encode(
    x = 'Date',
    y = r'U.S. Product Supplied of Normal Butane (Thousand Barrels per Day)'
)

I got this part of the code from the Altair Documentation

y = r'U.S. Product Supplied of Normal Butane (Thousand Barrels per Day)'

This is to mitigate the Chart appearing with empty content problem due to presence of special characters in Encodings

But I still get Error.

ValueError: U.S. Product Supplied of Normal Butane (Thousand Barrels per Day) encoding field is specified without a type; the type cannot be inferred because it does not match any column in the data.

Not sure what I am doing wrong.

Asked By: prashanth manohar

||

Answers:

It has to do with the dot (.) present in the name of the columns of your spreadsheet/dataframe and It seems that the escape (suggested by the documentation) does not work in your case.

As a workaround, you can remove the dot with pandas str.replace before using altair.Chart.

Try this :

import pandas as pd
import altair as alt

crude_df  = pd.read_excel("PET_CONS_PSUP_DC_NUS_MBBLPD_M.xlsx", sheet_name="Data 1", header=2)

crude_df.columns = crude_df.columns.str.replace(".", "", regex=True)

alt.Chart(crude_df.tail(100)).mark_circle().encode(
    x = 'Date',
    y = 'US Product Supplied of Normal Butane (Thousand Barrels per Day)'
)

# Output :

enter image description here

Answered By: abokey