Python pandas read_sql 返回生成器对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31170012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:33:31  来源:igfitidea点击:

Python pandas read_sql returns generator object

pythonsqlpandas

提问by DavidH

I am pulling data from Oracle db using pyodbc and pandas read_sql.

我正在使用 pyodbc 和 pandas read_sql 从 Oracle 数据库中提取数据。

I see no errors when I enter this line

当我输入这一行时,我没有看到任何错误

df = pd.read_sql(sql_str,cnxn,chunksize=10)

But when I try to see

但是当我试着去看

df

I get this error

我收到这个错误

<generator object _query_iterator at 0x092D40F8>

My search as to what this error means or what could be causing it yielded no satisfactory answers.

我对这个错误的含义或可能导致它的原因的搜索没有得到满意的答案。

The reason for using chunksize is that I have a Oracle db table with 60 million rows, and plan to download in bits and then put them together, just like here: How to create a large pandas dataframe from an sql query without running out of memory?

使用 chunksize 的原因是我有一个包含 6000 万行的 Oracle db 表,并计划按位下载然后将它们放在一起,就像这里一样: 如何从 sql 查询创建大型 Pandas 数据帧而不会耗尽内存?

回答by joris

As the explanation of chunksizesays, when specified, it returns an iterator where chunksize is the number of rows to include in each chunk.
So you can iterate through the result and do something with each chunk:

正如解释chunksize所说,当指定时,它返回一个迭代器,其中 chunksize 是每个块中包含的行数
所以你可以遍历结果并对每个块做一些事情:

for chunk in pd.read_sql_query(sql_str, engine, chunksize=10):
    do_something_with(chunk)

Typically you can process the chunk and add it to a list, and then after this for loop concat all processed chunks in this list together.

通常,您可以处理该块并将其添加到列表中,然后在此 for 循环之后将该列表中所有已处理的块连接在一起。

Also see the docs on sql querying: http://pandas.pydata.org/pandas-docs/stable/io.html#queryingfor an example.

另请参阅有关 sql 查询的文档:http: //pandas.pydata.org/pandas-docs/stable/io.html#querying例如。

回答by gffbss

I would prefer to comment but cannot yet. Regardless, that is not an error that is telling you that dfis a generator object.

我更愿意发表评论,但还不能。无论如何,这并不是告诉您这df是一个生成器对象的错误。