将 duckdb 结果转换为 Streamlit 支持的数据帧的更快方法?

问题描述 投票:0回答:1

不确定我是否在我的 Streamlit 应用程序中正确使用了 DuckDB。 在我的 Streamlit 中使用 duckdb 的全部目的是使其足够快速且响应灵敏。

import duckdb
import pandas as pd
import polars as pl
import time

conn = duckdb.connect()

# dummy.csv refers to a file that I created with 100 million rows for testing. 
3 gb dataset.

query = \
"""
CREATE TABLE dummy as (
select * from 'dummy.csv'
)
"""

# Creates table
conn.execute(query)

# Part 1: Testing with Duck DB show()
start_time = time.time()


query = \
"""
select * 
from dummy
"""

df = conn.sql(query).show()
df 
print("--- %s seconds ---" % (time.time() - start_time))

--- 0.013511896133422852 秒 --- duckdb 发挥其魔力。这就是我想要的速度。

start_time = time.time()

# Query 1
query = \
"""
select * 
from dummy
"""

# The reason i am doing this is to display the table in my streamlit application (This is just an illustration of how I convert duckdb results into dataframe which is the fed to streamlit application

df = conn.sql(query).df()
df
print("--- %s seconds ---" % (time.time() - start_time))

--- 6.356271505355835 秒 --- 太慢了,因为我相信我正在转换为 pandas 数据帧。

start_time = time.time()

# Query 1
query = \
"""
select * 
from dummy
"""

df = conn.execute(query).pl()
display(df)
print("--- %s seconds ---" % (time.time() - start_time))

--- 1.8795912265777588 秒 ---

好吧,这更好,但不高兴。

有没有更快的方法将数据转换为streamlit支持的dataframe?

st.dataframe(df)
pandas dataframe streamlit duckdb
1个回答
0
投票

来自 Python API 文档

查询结果作为关系返回。关系是查询的符号表示。

在获取结果或请求将结果打印到屏幕之前,查询不会执行

.show()
只是打印关系的摘要,它实际上不返回任何内容。

>>> foo = duckdb.sql("SELECT 1").show()
┌───────┐
│   1   │
│ int32 │
├───────┤
│     1 │
└───────┘
>>> type(foo)
NoneType

如上所述,查询未执行。

如果我们使用更大的查询,更容易看到我们只是得到一个“预览”

>>> duckdb.sql("from 'large-file.parquet'").show()
┌──────────────┬─────────────────┬───┬─────────────┬────────┬────────────┐
│ project_name │ project_version │ … │ skip_reason │ lines  │ repository │
│   varchar    │     varchar     │   │   varchar   │ uint64 │   uint32   │
├──────────────┼─────────────────┼───┼─────────────┼────────┼────────────┤
│ zz-pix       │ 0.0.7           │ … │             │      1 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │      2 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │      5 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │     17 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │    184 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │    201 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │     27 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │     45 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │     22 │        265 │
│ zz-pix       │ 0.0.7           │ … │             │      7 │        265 │
│  ·           │   ·             │ · │      ·      │      · │         ·  │
│  ·           │   ·             │ · │      ·      │      · │         ·  │
│  ·           │   ·             │ · │      ·      │      · │         ·  │
│ zrb          │ 0.11.0          │ … │             │      6 │        265 │
│ zrb          │ 0.11.0          │ … │             │     23 │        265 │
│ zrb          │ 0.11.0          │ … │             │    173 │        265 │
│ zrb          │ 0.11.0          │ … │             │    134 │        265 │
│ zrb          │ 0.11.0          │ … │             │     44 │        265 │
│ zrb          │ 0.11.0          │ … │             │     37 │        265 │
│ zrb          │ 0.11.0          │ … │             │      6 │        265 │
│ zrb          │ 0.11.0          │ … │             │      2 │        265 │
│ zrb          │ 0.11.0          │ … │             │      2 │        265 │
│ zrb          │ 0.11.0          │ … │             │      2 │        265 │
├──────────────┴─────────────────┴───┴─────────────┴────────┴────────────┤
│ ? rows (>9999 rows, 20 shown)                     11 columns (5 shown) │
└────────────────────────────────────────────────────────────────────────┘
  ^^^^^^

? rows
表示查询尚未执行,因为总行数未知。

使用

.pl()
,您将具体化将执行查询的结果集。

© www.soinside.com 2019 - 2024. All rights reserved.