Skip to content

Commit

Permalink
handle patching with NaT values (#5675)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Oct 18, 2023
1 parent 1c9df0e commit fab0ea7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from panel.io.state import set_curdoc
from panel.models.tabulator import CellClickEvent, TableEditEvent
from panel.tests.util import serve_and_request, wait_until
from panel.util import BOKEH_JS_NAT
from panel.widgets import Button, TextInput
from panel.widgets.tables import DataFrame, Tabulator

Expand Down Expand Up @@ -1295,6 +1296,28 @@ def test_tabulator_patch_with_timestamp(document, comm):
if col != 'index':
np.testing.assert_array_equal(table.value[col].values, expected[col])

def test_tabulator_patch_with_NaT(document, comm):
df = pd.DataFrame(dict(A=pd.to_datetime(['1980-01-01', np.nan])))
assert df.loc[1, 'A'] is pd.NaT
table = Tabulator(df)

model = table.get_root(document, comm)

table.patch({'A': [(0, pd.NaT)]})

# We're also checking that the NaT value that was in the original table
# at .loc[1, 'A'] is converted in the model as BOKEH_JS_NAT.
expected = {
'index': np.array([0, 1]),
'A': np.array([BOKEH_JS_NAT, BOKEH_JS_NAT])
}
for col, values in model.source.data.items():
expected_array = expected[col]
np.testing.assert_array_equal(values, expected_array)
# Not checking that the data in table.value is the same as expected
# In table.value we have NaT values, in expected the BOKEH_JS_NAT constant.


def test_tabulator_stream_series_paginated_not_follow(document, comm):
df = makeMixedDataFrame()
table = Tabulator(df, pagination='remote', page_size=2)
Expand Down
5 changes: 4 additions & 1 deletion panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from ..io.state import state
from ..reactive import Reactive, ReactiveData
from ..util import (
clone_model, datetime_as_utctimestamp, isdatetime, lazy_load, updating,
BOKEH_JS_NAT, clone_model, datetime_as_utctimestamp, isdatetime, lazy_load,
updating,
)
from .base import Widget
from .button import Button
Expand Down Expand Up @@ -824,6 +825,8 @@ def patch(self, patch_value, as_index=True):
self.value.iloc[data_ind, columns.index(k)] = value
if isinstance(value, pd.Timestamp):
value = datetime_as_utctimestamp(value)
elif value is pd.NaT:
value = BOKEH_JS_NAT
values.append((patch_ind, value))
patches[k] = values
self._patch(patches)
Expand Down

0 comments on commit fab0ea7

Please sign in to comment.