hvplot icon indicating copy to clipboard operation
hvplot copied to clipboard

DuckDb reference example not working

Open MarcSkovMadsen opened this issue 3 months ago • 2 comments

Example from https://hvplot.holoviz.org/en/docs/latest/

Image
import duckdb
import hvplot.duckdb
from bokeh.sampledata.autompg import autompg_clean as df

df_duckdb = duckdb.from_df(df)
table = df_duckdb.groupby(['origin', 'mfr'])['mpg'].mean().sort_values().tail(5)
table.hvplot.barh('mfr', 'mpg', by='origin', stacked=True)
Traceback (most recent call last):
  File "/home/jovyan/repos/private/panel/script.py", line 6, in <module>
    table = df_duckdb.groupby(['origin', 'mfr'])['mpg'].mean().sort_values().tail(5)
            ^^^^^^^^^^^^^^^^^
AttributeError: This relation does not contain a column by the name of 'groupby'

duckdb==1.2.2 hvplot==0.11.2

MarcSkovMadsen avatar Dec 01 '25 18:12 MarcSkovMadsen

This would work

import duckdb
import hvplot.duckdb
from bokeh.sampledata.autompg import autompg_clean as df

# Create a DuckDB connection and register the DataFrame as a table
con = duckdb.connect(':memory:')
con.register('autompg', df)

# Query using DuckDB SQL to get the same result
query = """
SELECT origin, mfr, mpg
FROM (
    SELECT origin, mfr, AVG(mpg) as mpg
    FROM autompg
    GROUP BY origin, mfr
    ORDER BY mpg DESC
    LIMIT 5
)
ORDER BY mpg ASC
"""

table = con.execute(query)
table.hvplot.barh('mfr', 'mpg', by='origin', stacked=True)
Image

MarcSkovMadsen avatar Dec 01 '25 18:12 MarcSkovMadsen

I would suggest using table = con.query(query) instead of con.execute(query) as this is supported by narwhals:

Image

hoxbro avatar Dec 02 '25 15:12 hoxbro