holoviz / panel

Panel: The powerful data exploration & web app framework for Python
https://panel.holoviz.org
BSD 3-Clause "New" or "Revised" License
4.58k stars 499 forks source link

Tabulator does not support editting Int64Dtype #3848

Open hoxbro opened 1 year ago

hoxbro commented 1 year ago

Pandas dtype for nullable integer is not currently supported in Tabulator when editing columns.

import numpy as np
import pandas as pd
import panel as pn
pn.extension("tabulator")

df = pd._testing.makeDataFrame()
df["B"][::5] = np.nan
df["B"] = df["B"].round().astype(pd.Int64Dtype())
pn.widgets.Tabulator(df)
Traceback (most recent call last):
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pyviz_comms/__init__.py", line 346, in _handle_msg
    self._on_msg(msg)
  File "/home/shh/Development/holoviz/repos/panel/panel/viewable.py", line 292, in _on_msg
    doc.unhold()
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/document.py", line 799, in unhold
    self.callbacks.unhold()
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 396, in unhold
    self.trigger_on_change(event)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 373, in trigger_on_change
    invoke_with_curdoc(doc, event.callback_invoker)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 408, in invoke_with_curdoc
    return f()
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/util/callback_manager.py", line 191, in invoke
    callback(attr, old, new)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 389, in _comm_change
    self._schedule_change(doc, comm)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 373, in _schedule_change
    self._change_event(doc)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 369, in _change_event
    self._process_events(events)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 1108, in _process_events
    self._process_data(events.pop('data'))
  File "/home/shh/Development/holoviz/repos/panel/panel/widgets/tables.py", line 1207, in _process_data
    return super()._process_data(data)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 1074, in _process_data
    values = self._convert_column(
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 1051, in _convert_column
    converted = values.astype(dtype)
TypeError: Cannot interpret 'Int64Dtype()' as a data type

https://user-images.githubusercontent.com/19758978/190601114-d917635c-8018-4ac4-8be9-0d5f67ee4950.mp4

hoxbro commented 1 year ago

The PR did not fix the issue, though it gives a new traceback:

Traceback (most recent call last):
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/arrays/integer.py", line 51, in _safe_cast
    return values.astype(dtype, casting="safe", copy=copy)
TypeError: Cannot cast array data from dtype('O') to dtype('int64') according to the rule 'safe'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pyviz_comms/__init__.py", line 346, in _handle_msg
    self._on_msg(msg)
  File "/home/shh/Development/holoviz/repos/panel/panel/viewable.py", line 292, in _on_msg
    doc.unhold()
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/document.py", line 799, in unhold
    self.callbacks.unhold()
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 396, in unhold
    self.trigger_on_change(event)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 373, in trigger_on_change
    invoke_with_curdoc(doc, event.callback_invoker)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/document/callbacks.py", line 408, in invoke_with_curdoc
    return f()
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/bokeh/util/callback_manager.py", line 191, in invoke
    callback(attr, old, new)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 393, in _comm_change
    self._schedule_change(doc, comm)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 377, in _schedule_change
    self._change_event(doc)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 373, in _change_event
    self._process_events(events)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 1117, in _process_events
    self._process_data(events.pop('data'))
  File "/home/shh/Development/holoviz/repos/panel/panel/widgets/tables.py", line 1258, in _process_data
    return super()._process_data(data)
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 1083, in _process_data
    values = self._convert_column(
  File "/home/shh/Development/holoviz/repos/panel/panel/reactive.py", line 1059, in _convert_column
    converted = pd.Series(values).astype(dtype).values
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/generic.py", line 6245, in astype
    new_data = self._mgr.astype(dtype=dtype, copy=copy, errors=errors)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/internals/managers.py", line 446, in astype
    return self.apply("astype", dtype=dtype, copy=copy, errors=errors)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/internals/managers.py", line 348, in apply
    applied = getattr(b, f)(**kwargs)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/internals/blocks.py", line 527, in astype
    new_values = astype_array_safe(values, dtype, copy=copy, errors=errors)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/dtypes/astype.py", line 299, in astype_array_safe
    new_values = astype_array(values, dtype, copy=copy)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/dtypes/astype.py", line 230, in astype_array
    values = astype_nansafe(values, dtype, copy=copy)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/dtypes/astype.py", line 95, in astype_nansafe
    return dtype.construct_array_type()._from_sequence(arr, dtype=dtype, copy=copy)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/arrays/masked.py", line 132, in _from_sequence
    values, mask = cls._coerce_to_array(scalars, dtype=dtype, copy=copy)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/arrays/numeric.py", line 258, in _coerce_to_array
    values, mask, _, _ = _coerce_to_data_and_mask(
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/arrays/numeric.py", line 214, in _coerce_to_data_and_mask
    values = dtype_cls._safe_cast(values, dtype, copy=False)
  File "/home/shh/miniconda3/envs/holoviz/lib/python3.9/site-packages/pandas/core/arrays/integer.py", line 57, in _safe_cast
    raise TypeError(
TypeError: cannot safely cast non-equivalent object to int64