SQLAlchemy ORM 转换为 Pandas DataFrame [英] SQLAlchemy ORM conversion to pandas DataFrame
问题描述
是否有将 SQLAlchemy
转换为 Pandas DataFrame 的解决方案?
Pandas 具有使用 pandas.read_sql
的能力,但这需要使用原始 SQL.我想避免它的原因有两个:
- 我已经拥有使用 ORM 的所有东西(这本身就是一个很好的理由)并且
- 我使用 python 列表作为查询的一部分,例如:
<块引用>
db.session.query(Item).filter(Item.symbol.in_(add_symbols)
其中 Item
是我的模型类和 add_symbols
> 是一个列表).这相当于 SQL SELECT ... from ... WHERE ... IN
.
有什么可能吗?
下面应该在大多数情况下工作:
df = pd.read_sql(query.statement, query.session.bind)
参见pandas.read_sql
有关参数的详细信息的文档.
Is there a solution converting a SQLAlchemy <Query object>
to a pandas DataFrame?
Pandas has the capability to use pandas.read_sql
but this requires use of raw SQL. I have two reasons for wanting to avoid it:
- I already have everything using the ORM (a good reason in and of itself) and
- I'm using python lists as part of the query, e.g.:
db.session.query(Item).filter(Item.symbol.in_(add_symbols)
whereItem
is my model class andadd_symbols
is a list). This is the equivalent of SQLSELECT ... from ... WHERE ... IN
.
Is anything possible?
Below should work in most cases:
df = pd.read_sql(query.statement, query.session.bind)
See pandas.read_sql
documentation for more information on the parameters.
这篇关于SQLAlchemy ORM 转换为 Pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!