jupyter-widgets / ipydatagrid

Fast Datagrid widget for the Jupyter Notebook and JupyterLab
BSD 3-Clause "New" or "Revised" License
580 stars 51 forks source link

🐛 `ipydatagrid>=1.3` doesn't support integer pandas column names #512

Closed jgunstone closed 6 months ago

jgunstone commented 7 months ago

... as unwanted column created with string name matching the integer on set_cell_value

I'm just trying to update ipyautoui to ipydatagrid>1.3.1 and came across this tricky little issue:

Describe the bug

a new column with the string name of the edited column is created. This only happens when retrieving the data in a new cell. image

To Reproduce

from ipydatagrid import DataGrid
import pandas as pd

df = pd.DataFrame({0:{"a":"a", "b": 1}, 1:{"a":"abba", "b": 2}})
display(df)
gr = DataGrid(df)
assert list(gr.data.columns) == [0, 1]
column_name, primary_key_value, new_value = 1, 'a', "my new string"
gr.set_cell_value(column_name, primary_key_value, new_value)
display(gr.data)
assert list(gr._data["data"]) == ["key", 0, 1, "ipydguuid"]
display(gr._data["data"].columns)
gr._data["data"]

Expected behavior Behaves as expected for non-integer column names

image

Environment (please complete the following information):

@martinRenou - maybe you could help?

jgunstone commented 7 months ago

feels like a weird jupyter related bug so I just tried with jupyterlab=4.2 https://github.com/jupyterlab/jupyterlab/issues/15801

and it worked! image

looks like that is slated for this week so I'll just hang tight until its out and try again

jgunstone commented 6 months ago

just tried with the jupyterlab==4.2 and unfortunately I'm getting the same original error

image

if I make the col names strings not numbers the bad behaviour stops, i.e. image

it seems as though on the JS side of ipydatagrid (or in lumino?) the integer col names are being coerced to strings and an additional column is added.

looking through the js side of things:

https://github.com/bloomberg/ipydatagrid/blob/23b6287f63c99e375d3aa09fb8eadeed9389129f/js/core/viewbasedjsonmodel.ts#L564-L573

https://github.com/bloomberg/ipydatagrid/blob/23b6287f63c99e375d3aa09fb8eadeed9389129f/js/core/viewbasedjsonmodel.ts#L205-L219

https://github.com/jupyterlab/lumino/blob/43ae67c5cfe90b04acff3bbdd12cce9bb7a879bf/packages/datagrid/src/jsonmodel.ts#L150-L158

it appears from the above that there is a requirement to use a string for the name (though this wasn't previously enforced). FYI the referenced frictionless schema spec doesn't explicitly require the column name to be a string.

Does ipydatagrid require the pandas DataFrame to have string col names? should it?