在 pandas 中为 read_sql 指定数据类型
Specifying dtypes for read_sql in pandas
我想指定执行 pandas.read_sql 时返回的数据类型。我特别感兴趣的是节省内存并将浮点值作为 np.float32 而不是 np.float64 返回。我知道之后我可以使用 astype(np.float32) 进行转换,但这并不能解决初始查询中大内存需求的问题。在我的实际代码中,我将提取 8400 万行,而不是此处显示的 5 行。 pandas.read_csv 允许将 dtypes 指定为字典,但我认为 read_sql 无法做到这一点。
我正在使用 MySQLdb 和 Python 2.7.
顺便说一句,read_sql 似乎使用了比最终 DataFrame 存储所需更多的内存 运行(大约 2 倍)。
In [70]: df=pd.read_sql('select ARP, ACP from train where seq < 5', connection)
In [71]: df
Out[71]:
ARP ACP
0 1.17915 1.42595
1 1.10578 1.21369
2 1.35629 1.12693
3 1.56740 1.61847
4 1.28060 1.05935
In [72]: df.dtypes
Out[72]:
ARP float64
ACP float64
dtype: object
cast() 和 convert() 怎么样?
'SELECT cast(ARP as float32()), cast (ACP as float32()) from train where seq < 5'
或类似的东西。
http://www.smallsql.de/doc/sql-functions/system/convert.html
看看this github issue,好像他们倾向于添加选项。
您可以使用 pandas read_sql_query 来指定返回的数据类型(仅自 pandas 1.3 起支持)。
pd.read_sql_query('select ARP, ACP from train where seq < 5', connection,
dtype={'ARP': np.float32, 'ACP': np.float32})
As an aside, read_sql seems to use far more memory while running (about 2x) than it needs for the final DataFrame storage.
也许你可以试试我们的工具 ConnectorX (pip install -U connectorx
),它是用 Rust 实现的,旨在提高 pandas.read_sql
在时间和内存使用方面的性能,并提供类似的界面。要切换到它,您只需要:
import connectorx as cx
conn_url = "mysql://username:password@server:port/database"
query = "select ARP, ACP from train where seq < 5"
df = cx.read_sql(conn_url, query)
pandas.read_sql
在 运行 期间使用大量内存的原因是因为它的中间 python 对象很大,在 ConnectorX
中我们使用 Rust 和流进程来解决这个问题。
这是一些基准测试结果:
PostgreSQL:
MySQL:
我想指定执行 pandas.read_sql 时返回的数据类型。我特别感兴趣的是节省内存并将浮点值作为 np.float32 而不是 np.float64 返回。我知道之后我可以使用 astype(np.float32) 进行转换,但这并不能解决初始查询中大内存需求的问题。在我的实际代码中,我将提取 8400 万行,而不是此处显示的 5 行。 pandas.read_csv 允许将 dtypes 指定为字典,但我认为 read_sql 无法做到这一点。
我正在使用 MySQLdb 和 Python 2.7.
顺便说一句,read_sql 似乎使用了比最终 DataFrame 存储所需更多的内存 运行(大约 2 倍)。
In [70]: df=pd.read_sql('select ARP, ACP from train where seq < 5', connection)
In [71]: df
Out[71]:
ARP ACP
0 1.17915 1.42595
1 1.10578 1.21369
2 1.35629 1.12693
3 1.56740 1.61847
4 1.28060 1.05935
In [72]: df.dtypes
Out[72]:
ARP float64
ACP float64
dtype: object
cast() 和 convert() 怎么样?
'SELECT cast(ARP as float32()), cast (ACP as float32()) from train where seq < 5'
或类似的东西。
http://www.smallsql.de/doc/sql-functions/system/convert.html
看看this github issue,好像他们倾向于添加选项。
您可以使用 pandas read_sql_query 来指定返回的数据类型(仅自 pandas 1.3 起支持)。
pd.read_sql_query('select ARP, ACP from train where seq < 5', connection,
dtype={'ARP': np.float32, 'ACP': np.float32})
As an aside, read_sql seems to use far more memory while running (about 2x) than it needs for the final DataFrame storage.
也许你可以试试我们的工具 ConnectorX (pip install -U connectorx
),它是用 Rust 实现的,旨在提高 pandas.read_sql
在时间和内存使用方面的性能,并提供类似的界面。要切换到它,您只需要:
import connectorx as cx
conn_url = "mysql://username:password@server:port/database"
query = "select ARP, ACP from train where seq < 5"
df = cx.read_sql(conn_url, query)
pandas.read_sql
在 运行 期间使用大量内存的原因是因为它的中间 python 对象很大,在 ConnectorX
中我们使用 Rust 和流进程来解决这个问题。
这是一些基准测试结果:
PostgreSQL:
MySQL: