ipydatagrid icon indicating copy to clipboard operation
ipydatagrid copied to clipboard

NaN are not displayed correctly when format is specified in TextRender

Open gioxc88 opened this issue 2 years ago • 2 comments

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

image

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='-'
    )
)

gioxc88 avatar Nov 03 '22 12:11 gioxc88

up

gioxc88 avatar Nov 25 '22 10:11 gioxc88

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")
)

martinRenou avatar Nov 25 '22 10:11 martinRenou

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")),
    ),
)

image

I think this issue can be closed.

tfmark avatar Apr 04 '24 07:04 tfmark