Skip to content

Commit

Permalink
Fix patching table with pd.Timestamp values (#5650)
Browse files Browse the repository at this point in the history
* fix patching with a dataframe without monotonic index

* fix patching pd.Timestamp values
  • Loading branch information
maximlt authored Oct 16, 2023
1 parent c58b384 commit 8e3e193
Show file tree
Hide file tree
Showing 2 changed files with 26 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 @@ -1271,6 +1271,29 @@ def test_tabulator_patch_ranges(document, comm):
if col != 'index':
np.testing.assert_array_equal(table.value[col].values, expected[col])

def test_tabulator_patch_with_timestamp(document, comm):
# https://github.com/holoviz/panel/issues/5555
df = pd.DataFrame(dict(A=pd.to_datetime(['1980-01-01', '1980-01-02'])))
table = Tabulator(df)

model = table.get_root(document, comm)

table.patch({'A': [(0, pd.Timestamp('2021-01-01'))]})

expected = {
'index': np.array([0, 1]),
'A': np.array(['2021-01-01T00:00:00.000000000',
'1980-01-02T00:00:00.000000000'],
dtype='datetime64[ns]')
}
for col, values in model.source.data.items():
if col == 'A':
expected_array = expected[col].astype(np.int64) / 10e5
else:
expected_array = expected[col]
np.testing.assert_array_equal(values, expected_array)
if col != 'index':
np.testing.assert_array_equal(table.value[col].values, expected[col])

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

0 comments on commit 8e3e193

Please sign in to comment.