hvplot
hvplot copied to clipboard
DuckDb reference example not working
Example from https://hvplot.holoviz.org/en/docs/latest/
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
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)
I would suggest using table = con.query(query) instead of con.execute(query) as this is supported by narwhals: