Repository: holoviz/holoviews
Pull request #6299 (2024-06-28 16:11:19)

Ensure no auto index is created when a kdim is part of a multi index
Fixes https://github.com/holoviz/holoviews/issues/6298

<details><summary>Code</summary>
<p>

```python
import numpy as np
import pandas as pd
import holoviews as hv

hv.extension('bokeh')

dti = pd.date_range(start='1/1/2018', end='1/08/2018')

other = list(range(len(dti)))

t = pd.DataFrame(
    dict(
        n=range(5, len(dti) + 5),
        m=range(10, len(dti) + 10),
    )
)
dfm = pd.MultiIndex.from_frame(t)
df = pd.DataFrame(np.random.randn(len(dfm)), index=dfm, columns=['test'])
df.head()

p = hv.Curve(df, 'm', 'test')
p
```

</p>
</details> 

![image](https://github.com/holoviz/holoviews/assets/35924738/fd77affd-a694-43e8-928f-21220c13d387)


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/holoviz/holoviews/pull/6299?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=holoviz) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 88.51%. Comparing base [(`febe075`)](https://app.codecov.io/gh/holoviz/holoviews/commit/febe07506bda2c433ac31938ce4c5aa62335559d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=holoviz) to head [(`965b2f3`)](https://app.codecov.io/gh/holoviz/holoviews/commit/965b2f389e5308074fde17283dd587c88eda3aee?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=holoviz).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #6299      +/-   ##
==========================================
- Coverage   88.52%   88.51%   -0.02%     
==========================================
  Files         323      323              
  Lines       67996    68000       +4     
==========================================
- Hits        60193    60188       -5     
- Misses       7803     7812       +9     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/holoviz/holoviews/pull/6299?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=holoviz).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=holoviz).


Patch:
diff --git a/holoviews/core/data/pandas.py b/holoviews/core/data/pandas.py
index a097bdf95..a667fd92d 100644
--- a/holoviews/core/data/pandas.py
+++ b/holoviews/core/data/pandas.py
@@ -72,7 +72,7 @@ class PandasInterface(Interface, PandasAPI):
 
             if kdims and not (len(kdims) == len(index_names) and {dimension_name(kd) for kd in kdims} == set(index_names)):
                 kdim = dimension_name(kdims[0])
-                if eltype._auto_indexable_1d and ncols == 1 and kdim not in data.columns:
+                if eltype._auto_indexable_1d and ncols == 1 and kdim not in [*data.columns, *cls.indexes(data)]:
                     data = data.copy()
                     data.insert(0, kdim, np.arange(len(data)))
 


Test patch:
diff --git a/holoviews/tests/core/data/test_pandasinterface.py b/holoviews/tests/core/data/test_pandasinterface.py
index bb2e520d7..d40eefc93 100644
--- a/holoviews/tests/core/data/test_pandasinterface.py
+++ b/holoviews/tests/core/data/test_pandasinterface.py
@@ -391,3 +391,9 @@ class PandasInterfaceMultiIndex(HeterogeneousColumnTests, InterfaceTests):
         assert list(grouped.keys()) == [0, 1, 2, 3]
         for k, v in grouped.items():
             pd.testing.assert_frame_equal(v.data, ds.select(values=k).data)
+
+    def test_regression_no_auto_index(self):
+        # https://github.com/holoviz/holoviews/issues/6298
+
+        plot = Scatter(self.df, kdims="number")
+        np.testing.assert_equal(plot.dimension_values('number'), self.df.index.get_level_values('number'))