Hi, this is a reply to the topic “Streaming with NaN values”, posted by @Meir_Tseitlin a while ago. The topic was closed without a specific solution being accepted by the user.
(Streaming with NaN values)
As I stumbled on exactly the same problem, and found Bryan’s suggestion useful, I’m posting how exactly I solved it.
The issue
When patching a DataFrame with ‘NaN’ string before turning it to dict and feeding to ColumnDataSource.stream…
stream(df.fillna(‘NaN’).to_dict(orient=“list”))
…the chart is not displayed properly. In may case:
a) filling NaN values with np.nan caused a JSON conversion error;
b) filling NaN values with ‘’ strings did avoid the error, BUT values were treated as zeroes, which showed wrong lines in my chart.
My solution
What worked for me was:
- Make sure my NaN values were numpy’s NaN with df.fillna(np.nan, inplace=True)
- Convert the columns passed in the dictionary into NumPy arrays like this:
stream(df.to_dict(‘series’)).
Alternative conversions of the columns like to_dict(‘list’) didn’t work well for the reasons above.
Hope it helps,
Alberto H