Pn widget button for date

Dear All,

How can we use date in pn widget button since If I put date in name widget will error ,
sample code:

import pandas as pd
import numpy as np
import panel as pn
pn.extension()
# Creating a synthetic DataFrame
data = {
    'date': pd.date_range(start='2022-01-01', end='2022-01-05'),
    'brand': np.random.choice(['Brand A', 'Brand B', 'Brand C'], size=5),
    'shipment_destination': np.random.choice(['Destination X', 'Destination Y'], size=5)
}

df = pd.DataFrame(data)

# Finding the latest date, brand, and shipment destination
latest_index = df['date'].idxmax()
latest_brand = df.loc[latest_index, 'brand']
latest_shipment_destination = df.loc[latest_index, 'shipment_destination']
latest_date = df.loc[latest_index, 'date']

pn.widgets.Button(icon='calendar-month', button_type='warning', name=latest_date,icon_size='2em')

error message:

File ~/.local/lib/python3.9/site-packages/param/parameterized.py:1634, in String._validate_value(self, val, allow_None)
   1632     return
   1633 if not isinstance(val, str):
-> 1634     raise ValueError(
   1635         f'{_validate_error_prefix(self)} only takes a string value, '
   1636         f'not value of {type(val)}.'
   1637     )

ValueError: String parameter 'Button.name' only takes a string value, not value of <class 'pandas._libs.tslibs.timestamps.Timestamp'>.

thanks

1 Like

I’m not quite sure what you are trying to do with a button widget. Are you trying to select a time value? If so perhaps a selection widget is what you want?

If you use groupby='time' then hvplot will generate a widget for you:

import hvplot.pandas
import pandas as pd
import numpy as np
import panel as pn
pn.extension()
# Creating a synthetic DataFrame
data = {
    'date': pd.date_range(start='2022-01-01', end='2022-01-05'),
    'brand': np.random.choice(['Brand A', 'Brand B', 'Brand C'], size=5),
    'shipment_destination': np.random.choice(['Destination X', 'Destination Y'], size=5)
}

df = pd.DataFrame(data)
df.hvplot.scatter(x='shipment_destination', y='brand', groupby='date', grid=True)

produces:

1 Like

str(latest_date) should stop it from erroring.

1 Like

hi @ahuang11 thanks perfect

Thanks @rsignell , I just want to give information to user the latest date of shipment currently exist in the dataframe. Anyway thanks for your code. Regarding your code , is that possible to custom date widget which is currently in the right plot become in the top or anywhere we want?

thanks

Ah, okay. I was focused on how I usually use widgets I didn’t understand your goal! And yes, you can put it anywhere you want using hvplot option widget_location: Widgets — hvPlot 0.9.1 documentation (holoviz.org)

Hi @rsignell perfect. thanks for hvplot documentation .