Skip to content

Impossible to format dates using Expr (please provide example of how to do it ) #372

@gioxc88

Description

@gioxc88

I tried multiple times to format datetime because I want to avoid displaying NaT and I find it impossible despite trying in many different ways.
If it wasn't for NA I could have just used TextRenderer(format='%Y%B%d', format_type='time')

Here is a simple example:

import pandas as pd
from ipydatagrid import TextRenderer, DataGrid
from py2vega.functions.type_coercing import toDate
from py2vega.functions.formatting import timeFormat, timeParse
from py2vega.functions.type_checking import isValid, isDate, isString


def format_date(cell):
    if not isValid(cell.value):
        return '-'
    else:
        return timeFormat(toDate(cell.value), '%Y%B%d')

    
date = pd.date_range(start='20221010', periods=10, name='date').to_frame().reset_index(drop=True)
date.iloc[0,0] = np.nan


DataGrid(
    date,
    default_renderer=TextRenderer(text_value=Expr(format_date))
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions