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

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 https://dash.plot.ly/datatable/typing.

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.

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

Just bumping this topic. I’m not understanding how I can format a datetime column while still allowing it to be sorted correctly.

Something like:

columns=[{'id':'Date','name':'My Formatted Date','type': 'datetime', 'format:'???'}]

But I’m not understanding what that ??? should be.

According to https://dash.plotly.com/datatable/reference, columns.format is “derived from the d3-format library specification”, which doesn’t have any date-formatting functionality. So that definitely won’t work. If you change the date column of the DataFrame into a string with the format YYYY-MM-DD, before you send the DataFrame to the Datatable, then it will display correctly and sort correctly, because alphabetical order on the string is the same as chronological order.

For me the way it works best is to convert the data in pandas to dt.date format:
df[date_col] = df[date_col].dt.date

When you move this to datatable it keeps just YYYY-MM-DD with no time and it still is a date.