0

I'm trying to build a conda panel that can see values based on DatetimeRangeInput slider

so I created dataframe like this

data = {'name':['A', 'B', 'C', 'D'],
        'open_time': [dt.datetime(2021, 7, 1, 21, 0), dt.datetime(2021, 7, 2, 11, 0), dt.datetime(2021, 7, 3, 3, 0),dt.datetime(2021, 7, 3, 2, 0) ],
        'count':[27, 24, 22, 32],
        'size':[34, 56,67,78]
       }

df_sample = pd.DataFrame(data)
df_sample

so the output like this

   name open_time          count size
0   A   2021-07-01 21:00:00 27   34
1   B   2021-07-02 11:00:00 24   56
2   C   2021-07-03 03:00:00 22   67
3   D   2021-07-03 02:00:00 32   78

then added y axis options in this way

yaxis_sample = pn.widgets.RadioButtonGroup(
    name='Y axis', 
    options=['count', 'size'],
    button_type='success'
)

widget created like this

date_range = pn.widgets.DateRangeSlider(
    name='Date Range',
    start=dt.datetime(2021, 7, 1, 00, 00, 00), end=dt.datetime(2021, 8, 1, 00, 00, 00),
    value=(dt.datetime(2021, 7, 2, 00, 00, 00), dt.datetime(2021, 7, 4, 00, 00, 00)),
)

date_range

enter image description here then I'm struggling to combine pipeline and widget, in this section

names = ['A', 'B','C', 'D']

sample_pipeline = (
    df[
        (date_range.align(df_sample.open_time)) &
        (df_sample.name.isin(names))
    ]
    .groupby(['name', 'open_time'])[yaxis_sample].mean()
    .to_frame()
    .reset_index()
    .sort_values(by='open_time')  
    .reset_index(drop=True)
)

how to check df_sample.open_time is in the range of date_range.value ?

I've tried following

Attempt 1

names = ['A', 'B','C', 'D']

sample_pipeline = (
    df[
        (df_sample.open_time >= date_range.param.value_start) & (df_sample.open_time <= date_range.param.value_end) &
        (df_sample.name.isin(names))
    ]
    .groupby(['name', 'open_time'])[yaxis_sample].mean()
    .to_frame()
    .reset_index()
    .sort_values(by='open_time')  
    .reset_index(drop=True)
)

TypeError: Invalid comparison between dtype=datetime64[ns] and Date

Attempt 2

names = ['A', 'B','C', 'D']

sample_pipeline = (
    df[
        (df_sample.open_time >= date_range.start) & (df_sample.open_time <= date_range.end) &
        (df_sample.name.isin(names))
    ]
    .groupby(['name', 'open_time'])[yaxis_sample].mean()
    .to_frame()
    .reset_index()
    .sort_values(by='open_time')  
    .reset_index(drop=True)
)

IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

Kelum
  • 1,745
  • 5
  • 24
  • 45

1 Answers1

0

It wasn't clear in your post, but something you have to do is instantiate an interactive dataframe with df.interactive(), .interactive being a dataframe accessor that is registered by hvPlot to on Pandas dataframes when importign hvplot.pandas.

Then indeed you were right in your first attempt, the trick with the date ranges is to rely on the Parameter value_start and value_end.

import datetime as dt
import hvplot.pandas
import pandas as pd
import panel as pn

# Data init
data = {'name':['A', 'B', 'C', 'D'],
        'open_time': [dt.datetime(2021, 7, 1, 21, 0), dt.datetime(2021, 7, 2, 11, 0), dt.datetime(2021, 7, 3, 3, 0),dt.datetime(2021, 7, 3, 2, 0) ],
        'count':[27, 24, 22, 32],
        'size':[34, 56,67,78]
       }
df = pd.DataFrame(data)

names = ['A', 'B','C', 'D']

# Create an interactive dataframe
dfi = df.interactive()

# Create the widgets that will drive the pipeline

date_range = pn.widgets.DateRangeSlider(
    name='Date Range',
    start=dt.datetime(2021, 7, 1, 00, 00, 00), end=dt.datetime(2021, 8, 1, 00, 00, 00),
    value=(dt.datetime(2021, 7, 2, 00, 00, 00), dt.datetime(2021, 7, 4, 00, 00, 00)),
)
yaxis_sample = pn.widgets.RadioButtonGroup(
    name='Y axis', 
    options=['count', 'size'],
    button_type='success'
)

# Create the pipeline
sample_pipeline = (
    dfi[
        (dfi.open_time >= date_range.param.value_start) & (dfi.open_time <= date_range.param.value_end) &
        (dfi.name.isin(names))
    ]
    .groupby(['name', 'open_time'])[yaxis_sample].mean()
    .to_frame()
    .reset_index()
    .sort_values(by='open_time')  
    .reset_index(drop=True)
)
sample_pipeline

enter image description here

MaximeL
  • 391
  • 3
  • 7