ipydatagrid
ipydatagrid copied to clipboard
NaN are not displayed correctly when format is specified in TextRender
if I have nan in the dataframe of even NaT for dates, there is no way to change the display value for these cells.
There is a parameter called missing
in the TextRenderer
but it does nothing
is there an alternative way to specify the value for NaN?
Thanks
import numpy as np
import pandas as pd
from ipydatagrid import DataGrid, TextRenderer
df = pd.DataFrame(np.random.uniform(size=(10, 2)))
df.iloc[0, 1] = np.nan
DataGrid(
df,
default_renderer=TextRenderer(
format='.2f',
missing='-'
)
)
up
You should be able to use a conditional formatter using the Math.isNan
function:
Using a Vega expression:
from ipydatagrid import VegaExpr, TextRenderer
TextRenderer(
text_value=VegaExpr("Math.isNan(cell.value) ? '-' : cell.value")
)
Using a Python expression:
from ipydatagrid import Expr, TextRenderer
TextRenderer(
text_value=Expr("'-' if Math.isNan(cell.value) else cell.value")
)
The following code works (VegaExpr or Expr) to replace NaN with a hyphen:
import ipydatagrid
import pandas as pd
import numpy as np
ipydatagrid.DataGrid(
dataframe=pd.DataFrame({"a":[np.nan, 2, 3], "b":[4, np.nan, 6], "c":[7, 8, np.nan]}),
renderers=dict(
a=ipydatagrid.TextRenderer(text_value=ipydatagrid.VegaExpr("isNaN(cell.value) ? '-' : cell.value")),
b=ipydatagrid.TextRenderer(text_value=ipydatagrid.Expr("cell.value if isValid(cell.value) else '-'")),
c=ipydatagrid.TextRenderer(text_value=ipydatagrid.Expr("'-' if math.isNaN(cell.value) else cell.value")),
),
)
I think this issue can be closed.