Black Lives Matter. Please consider donating to Black Girls Code today.

Datatable datetime format

I have data from a pandas dataframe that I want to put in a datatable. The first field is a date. It’s formatted YYYY-MM-DD in pandas. In the dataframe it’s format is YYYY-MM-DDT00:00:00. How do I get time out of the format in the dataframe? In all of the examples in the DataTable user guide dates are in YYYY-MM-DD format.

This worked properly in an earlier version of Dash and DataTable (I believe the version of Dash was 0.4).

I’m also having this issue. Anyone found a fix?

I hit all my date columns with this before populating the DataTable component:

for x in ["Date Col1", "Date Col2", "Date Col3"]:
        if x in df.columns:
            df[x] = pd.to_datetime(df[x], format="%Y-%m-%d").dt.floor("d")
1 Like

That doesn’t work for me unfortunately, Dash still adds the extra ‘00:00:00’.

Assuming that your column is named as “Date_Col”, you can format it before creating the data table:

df.Date_Col = pd.DatetimeIndex(df.Date_Col).strftime("%Y-%m-%d")

That should do it.

Also refer to this documentation for further details

1 Like

Thanks that worked! I’ll check out that documentation

Sorting doesn’t work since strftime converts to string

I haven´t yet found a ‘format’ solution in the table that allows to sort by date.

I used the index numbers of the df (sorted by datetime) as an “id” column in the table to sort by date.