Skip to content

Display an informative DataFrame index in table plots - #1783

Open
fatimaLaraibBonk wants to merge 1 commit into
holoviz:mainfrom
fatimaLaraibBonk:fix-table-custom-index
Open

fatimaLaraibBonk wants to merge 1 commit into
holoviz:mainfrom
fatimaLaraibBonk:fix-table-custom-index

Conversation

@fatimaLaraibBonk

Copy link
Copy Markdown

Fixes #1734

df.hvplot.table() drops the DataFrame index unless it is a DatetimeIndex or a MultiIndex. A string index, such as names, IDs or categories, disappears from the table, so the rows can no longer be identified. This happens because Bokeh doesn't render the index, and hvPlot only resets it into a column for those two index types.

Reproduction

import pandas as pd
import hvplot.pandas  # noqa

df = pd.DataFrame(
    {"sales": [120, 95, 143], "profit": [30.5, 12.0, 41.2]},
    index=pd.Index(["Lahore", "Karachi", "Islamabad"], name="city"),
)
df.hvplot.table()

Before: the table only has sales and profit, and the city column is missing.
After: the table has city, sales and profit.

What decides whether the index is shown

The index is reset into a column when it carries information: a MultiIndex, a named index, or any non-integer index (strings, categories, datetimes). An unnamed integer index is still hidden, because it only holds row numbers and the table already shows those. Tables of a plain DataFrame or a filtered DataFrame therefore look the same as before.

Tests

New cases in hvplot/tests/kinds/test_table.py cover named and unnamed string indexes, a named integer index and a categorical index (all fail on main), and check that a RangeIndex and an unnamed integer index stay hidden. The full test suite passes (2181 passed), with the same xpassed tests as main.

df.hvplot.table() only displayed the index for a DatetimeIndex or a
MultiIndex. Any other index, such as a named index or an index of
strings, was dropped from the table, because Bokeh does not render the
DataFrame index. Display the index whenever it carries information, and
keep hiding an unnamed integer index, which only holds row numbers.

Fixes holoviz#1734

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

table plotted from a dataframe does not use custom indices but replaces row headers with integer indices.

1 participant