jupyter-widgets / ipydatagrid

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

transforms by column name do not work #554

Open ralbright-insiderscore opened 3 weeks ago

ralbright-insiderscore commented 3 weeks ago
import pandas as pd
import ipydatagrid
import traitlets
print(ipydatagrid.__version__)
print(traitlets.__version__)
print(pd.__version__)
test_df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
grid = ipydatagrid.DataGrid(test_df)
grid.transform(
    [
        {"type": "filter", "operator": ">", "column": "b", "value": 5},
    ])
display(grid)
1.3.2
5.14.3
2.2.3

KeyError                                  Traceback (most recent call last)
Cell In[94], line 5
      3 test_df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
      4 grid = ipydatagrid.DataGrid(test_df)
----> 5 grid.transform(
      6     [
      7         {"type": "filter", "operator": ">", "column": "b", "value": 5},
      8     ])
      9 display(grid)

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/ipydatagrid/datagrid.py:702](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/ipydatagrid/datagrid.py#line=701), in DataGrid.transform(self, transforms)
    700 """Apply a list of transformation to this DataGrid."""
    701 # TODO: Validate this input, or let it fail on view side?
--> 702 self._transforms = transforms

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py:716](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py#line=715), in TraitType.__set__(self, obj, value)
    714 if self.read_only:
    715     raise TraitError('The "%s" trait is read-only.' % self.name)
--> 716 self.set(obj, value)

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py:3635](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py#line=3634), in List.set(self, obj, value)
   3633     return super().set(obj, [value])  # type:ignore[list-item]
   3634 else:
-> 3635     return super().set(obj, value)

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py:690](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py#line=689), in TraitType.set(self, obj, value)
    689 def set(self, obj: HasTraits, value: S) -> None:
--> 690     new_value = self._validate(obj, value)
    691     assert self.name is not None
    692     try:

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py:724](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py#line=723), in TraitType._validate(self, obj, value)
    722     value = self.validate(obj, value)
    723 if obj._cross_validation_lock is False:
--> 724     value = self._cross_validate(obj, value)
    725 return t.cast(G, value)

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py:730](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py#line=729), in TraitType._cross_validate(self, obj, value)
    728 if self.name in obj._trait_validators:
    729     proposal = Bunch({"trait": self, "value": value, "owner": obj})
--> 730     value = obj._trait_validators[self.name](obj, proposal)
    731 elif hasattr(obj, "_%s_validate" % self.name):
    732     meth_name = "_%s_validate" % self.name

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py:1241](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/traitlets/traitlets.py#line=1240), in EventHandler.__call__(self, *args, **kwargs)
   1239 """Pass `*args` and `**kwargs` to the handler's function if it exists."""
   1240 if hasattr(self, "func"):
-> 1241     return self.func(*args, **kwargs)
   1242 else:
   1243     return self._init_call(*args, **kwargs)

File [~/anaconda3/envs/analysis/lib/python3.9/site-packages/ipydatagrid/datagrid.py:828](http://localhost:8888/home/ralbright/anaconda3/envs/analysis/lib/python3.9/site-packages/ipydatagrid/datagrid.py#line=827), in DataGrid._validate_transforms(self, proposal)
    826 field_len = len(self._data["schema"]["fields"])
    827 for transform in transforms:
--> 828     if transform["columnIndex"] > field_len:
    829         raise ValueError("Column index is out of bounds.")
    830 return transforms

KeyError: 'columnIndex'