From ab58d4b3318d84b227f2d4bee80e33db1e4e90b8 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Thu, 2 Apr 2020 15:49:47 +0200 Subject: [PATCH] Fixed DataFrame widget datetime formatting (#1221) * Fixed DataFrame widget datetime formatting * Add test --- panel/tests/widgets/test_tables.py | 16 +++++++++++++++- panel/widgets/tables.py | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/panel/tests/widgets/test_tables.py b/panel/tests/widgets/test_tables.py index 0070d81bda..64e8411e8b 100644 --- a/panel/tests/widgets/test_tables.py +++ b/panel/tests/widgets/test_tables.py @@ -4,12 +4,13 @@ try: import pandas as pd + from pandas.util.testing import makeTimeDataFrame except ImportError: pytestmark = pytest.mark.skip('pandas not available') from bokeh.models.widgets.tables import ( NumberFormatter, IntEditor, NumberEditor, StringFormatter, - SelectEditor + SelectEditor, DateFormatter, DateEditor ) from panel.widgets import DataFrame @@ -40,6 +41,19 @@ def test_dataframe_widget(dataframe, document, comm): assert isinstance(float_col.editor, NumberEditor) +def test_dataframe_widget_datetimes(document, comm): + + table = DataFrame(makeTimeDataFrame()) + + model = table.get_root(document, comm) + + dt_col, _, _, _, _ = model.columns + + assert dt_col.title == 'index' + assert isinstance(dt_col.formatter, DateFormatter) + assert isinstance(dt_col.editor, DateEditor) + + def test_dataframe_editors(dataframe, document, comm): editor = SelectEditor(options=['A', 'B', 'C']) table = DataFrame(dataframe, editors={'str': editor}) diff --git a/panel/widgets/tables.py b/panel/widgets/tables.py index 993903b059..01b276b074 100644 --- a/panel/widgets/tables.py +++ b/panel/widgets/tables.py @@ -68,12 +68,13 @@ def _get_columns(self): elif kind == 'f': formatter = NumberFormatter(format='0,0.0[00000]') editor = NumberEditor() - elif isdatetime(data): + elif isdatetime(data) or kind == 'M': formatter = DateFormatter(format='%Y-%m-%d %H:%M:%S') editor = DateEditor() else: formatter = StringFormatter() editor = StringEditor() + if col in self.editors: editor = self.editors[col] if col in self.formatters: