Search code examples
pythonpython-3.xmypyaltairvega-lite

Python Altair, `mark_line()`, _ignore_ NaN instead of _skipping_ them or treating them as 0?


As far as I can see, for example here, the only two ways Altair handles NaNs is either "filter" or None. In the context of mark_line(), "filter" means that the NaN will be skipped, which means that there will be a break in the line, and None means that NaN would be treated as zero, which would be insane in my application LOL. Is there a way to make Altair ignore, not skip, NaNs, when drawing a line? So that the line isn't broken in the middle?

I know I can always prune out the NaNs from the Pandas Dataframe by hand before plotting the line, but that would be incredibly inconvenient when there are several columns that I want to plot at the same time, each having NaNs in different places. It would be nearly impossible, as far as I know, to plot that and have nice things like a hover tool that shows all column values at the same time and so on.


Solution

  • It sounds like you want to use .transform_filter('isValid(datum.line)') as these two answers:

    import pandas as pd
    import altair as alt
    
    
    pd.DataFrame({
        'date': ['2020-04-03', '2020-04-04', '2020-04-05', '2020-04-06','2020-04-03', '2020-04-04','2020-04-05','2020-04-06'],
        'ID': ['a','a','a','a','b','b','b','b'],
        'line': [8,np.nan,10,8, 4, 5,6,7]
    }).pipe(
        alt.Chart
    ).mark_line(point=True).encode(
        alt.X('monthdate(date):O'),
        y='line:Q'
    ).transform_filter(
        'isValid(datum.line)'
    )
    

    enter image description here