enthought / enthought/traitsui

DataFrameAdapter breaks TabularEditor and Pandas when using plain string column names

Open
#1,747 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
306
Forks
99
PR merge metrics
No merged PRs in 30d

Description

Given that `DataFrameAdapter` inherit from `TabularAdapter` I thought I would be able to specify column names as a list of things, but doing so leads to a hard crash (see below). Providing a `(Str, Str)` pair works fine.

```python
from traitsui.ui_editors.data_frame_editor import DataFrameEditor, DataFrameAdapter
from traitsui.api import View, Item
from traits.api import HasTraits, Instance, observe
import pandas as pd

class App(HasTraits):
df = Instance(pd.DataFrame, args=())
editor = Instance(DataFrameEditor)
adapter = Instance(DataFrameAdapter, args=())

def default_traits_view(self):
return View(
Item('df', editor=self.editor),
resizable=True,
)

def _editor_default(self):
return DataFrameEditor(adapter=self.adapter)

@observe("df")
def read(self, event):
# Causes hard crash
self.adapter.columns = [str(col) for col in self.df.columns]
# Works
# self.adapter.columns = [(str(col), str(col)) for col in self.df.columns]

if __name__ == "__main__":
df = pd.DataFrame([[0, 1], [2, 3]], columns=['A', 'B'], index=[0, 1])
app = App()
app.df = df
app.configure_traits()
```

Running the above causes Python to hard crash:

```sh
=> python crash_causing.py
Traceback (most recent call last):
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2897, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/traitsui/qt4/tabular_model.py", line 81, in data
string = adapter.get_alignment(obj, name, column)
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/traitsui/tabular_adapter.py", line 261, in get_alignment
return self._result_for("get_alignment", object, trait, 0, column)
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/traitsui/tabular_adapter.py", line 738, in _result_for
return handler()
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/traitsui/tabular_adapter.py", line 746, in
return lambda: getattr(self, name)
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/traitsui/ui_editors/data_frame_editor.py", line 79, in _get_alignment
column = self.item[self.column_id]
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/pandas/core/frame.py", line 2995, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/achabot/.edm/envs/python-class/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2899, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 0
fish: Job 1, 'python crash_causing.py' terminated by signal SIGABRT (Abort)
```

Package info

```sh
=> edm list | grep traits
traits 6.2.0-1 enthought/free
traitsui 7.2.1-1 enthought/free
=> edm list | grep pandas
pandas 0.25.3-3 enthought/free
=> python --version
Python 3.6.12 -- Enthought, Inc. (x86_64)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.