Skip to content

Commit

Permalink
Allow index as column name in table widgets (#2990)
Browse files Browse the repository at this point in the history
* Allow index as column name in table widgets

* Handle unnamed indexes
  • Loading branch information
philippjfr authored Dec 7, 2021
1 parent eb81f59 commit be4460c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
28 changes: 28 additions & 0 deletions panel/tests/widgets/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ def test_hierarchical_index(document, comm):
assert isinstance(agg2, MinAggregator)


def test_table_index_column(document, comm):
df = pd.DataFrame({
'int': [1, 2, 3],
'float': [3.14, 6.28, 9.42],
'index': ['A', 'B', 'C'],
}, index=[1, 2, 3])
table = DataFrame(value=df)

model = table.get_root(document, comm=comm)

assert np.array_equal(model.source.data['level_0'], np.array([1, 2, 3]))
assert model.columns[0].field == 'level_0'
assert model.columns[0].title == ''


def test_none_table(document, comm):
Expand Down Expand Up @@ -288,6 +301,21 @@ def test_tabulator_expanded_content():
assert row2.text == "<pre>2.0</pre>"


def test_tabulator_index_column(document, comm):
df = pd.DataFrame({
'int': [1, 2, 3],
'float': [3.14, 6.28, 9.42],
'index': ['A', 'B', 'C'],
}, index=[1, 2, 3])
table = Tabulator(value=df)

model = table.get_root(document, comm=comm)

assert np.array_equal(model.source.data['level_0'], np.array([1, 2, 3]))
assert model.columns[0].field == 'level_0'
assert model.columns[0].title == ''


def test_tabulator_expanded_content_pagination():
df = makeMixedDataFrame()

Expand Down
26 changes: 20 additions & 6 deletions panel/widgets/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ def _get_column_definitions(self, col_names, df):
title = self.titles.get(col, str(col))
if col in indexes and len(indexes) > 1 and self.hierarchical:
title = 'Index: %s' % ' | '.join(indexes)
elif col in self.indexes and col.startswith('level_'):
title = ''
column = TableColumn(field=str(col), title=title,
editor=editor, formatter=formatter,
**col_kwargs)
Expand Down Expand Up @@ -409,9 +411,13 @@ def _get_data(self):
if df is None:
return [], {}
if isinstance(self.value.index, pd.MultiIndex):
indexes = list(df.index.names)
indexes = [
f'level_{i}' if n is None else n
for i, n in enumerate(df.index.names)
]
else:
indexes = [df.index.name or 'index']
default_index = ('level_0' if 'index' in df.columns else 'index')
indexes = [df.index.name or default_index]
if len(indexes) > 1:
df = df.reset_index()
data = ColumnDataSource.from_df(df)
Expand All @@ -432,8 +438,12 @@ def indexes(self):
if self.value is None or not self.show_index:
return []
elif isinstance(self.value.index, pd.MultiIndex):
return list(self.value.index.names)
return [self.value.index.name or 'index']
return [
f'level_{i}' if n is None else n
for i, n in enumerate(self.value.index.names)
]
default_index = ('level_0' if 'index' in self.value.columns else 'index')
return [self.value.index.name or default_index]

def stream(self, stream_value, rollover=None, reset_index=True):
"""
Expand Down Expand Up @@ -990,9 +1000,13 @@ def _get_data(self):
start = (self.page-1)*nrows
page_df = df.iloc[start: start+nrows]
if isinstance(self.value.index, pd.MultiIndex):
indexes = list(df.index.names)
indexes = [
f'level_{i}' if n is None else n
for i, n in enumerate(df.index.names)
]
else:
indexes = [df.index.name or 'index']
default_index = ('level_0' if 'index' in df.columns else 'index')
indexes = [df.index.name or default_index]
if len(indexes) > 1:
page_df = page_df.reset_index()
data = ColumnDataSource.from_df(page_df).items()
Expand Down

0 comments on commit be4460c

Please sign in to comment.