hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

No automatic sorting of multilevel datetime indexes

Open maximlt opened this issue 1 year ago • 0 comments

hvPlot automatically sorts x for Pandas when it's a datetime series, feature which can be disabled by setting sort_date=False.

The code looks like it's trying to support multilevel indexes but it doesn't work:

            if x in self.indexes:
                index = self.indexes.index(x)
                if is_datetime(data.axes[index]):
                    data = data.sort_index(axis=self.indexes.index(x))
import numpy as np
import pandas as pd
import hvplot.pandas

dti = pd.date_range(start='1/1/2018', end='1/08/2018')
dti

other = list(range(len(dti)))

t = pd.DataFrame(dict(dt=pd.date_range(start='1/1/2018', end='1/08/2018'), n=range(len(dti))))
dfm = pd.MultiIndex.from_frame(t)
dfm

df = pd.DataFrame(np.random.randn(len(dfm)), index=dfm)
df.head()

df_shuffled = df.sample(n=len(df))
df_shuffled.head()

p = df_shuffled.hvplot.line('dt')
print(p)
p
image

It does work when the index is not multilevel.

p2 = df_shuffled.droplevel(1, axis=0).hvplot.line('dt', 0)
print(p2)
p2
image

maximlt avatar Jun 27 '24 13:06 maximlt