Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It is not clear whether database connection need to be closed manually after calling pd.read_sql() #23086

Closed
FukoH opened this issue Oct 11, 2018 · 10 comments · Fixed by #29373
Closed
Labels
Docs good first issue IO SQL to_sql, read_sql, read_sql_query

Comments

@FukoH
Copy link

FukoH commented Oct 11, 2018

The document does not mention it.

@verascity
Copy link

I think this is a good point. Maintainers, correct me if I'm wrong (I'm a new contributor), but I believe it does have to be manually closed. I had to search for a bit to verify that, though.

If confirmed, could I take on clarifying in the docs?

@CalmDownKarm
Copy link
Contributor

If I understand correctly, you shouldn't need to manually close connections, sqlalchemy's create_engine() essentially handles connections through a connection pool automatically. calling dispose() on a create_engine()'d object will just replace it with an empty connection pool. sqlalchemy docs

@TomAugspurger
Copy link
Contributor

The question from @FukoH is a bit unclear though. @CalmDownKarm is correct about sqlalchemy connection pooling. We also support raw connections for sqlite (though that's maybe deprecated?).

Regardless, I don't think pandas closes anything internally. A link to the sqlalchemy docs in read_sql and to_sql may would be nice to have.

@TomAugspurger TomAugspurger modified the milestones: 0.24.0, Contributions Welcome Oct 12, 2018
@verascity
Copy link

The sqlite info is a little ambiguous. On read_sql it seems to imply sqlite is non-preferred but supported:

con : SQLAlchemy connectable(engine/connection) or database string URI or DBAPI2 connection (fallback mode)
Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

But on to_sql, it outright says sqlite is only supported as legacy.

con : sqlalchemy.engine.Engine or sqlite3.Connection
Using SQLAlchemy makes it possible to use any DB supported by that library. Legacy support is provided for sqlite3.Connection objects.

So maybe the read_sql doc should also be updated to reflect that, and then there can be a link to the SQLAlchemy docs in both?

@CalmDownKarm
Copy link
Contributor

@verascity are you planning on submitting a PR for this?

@verascity
Copy link

I'd like to, yes! Just wanted to get it pinned down before I do it.

@TomAugspurger
Copy link
Contributor

I think we're OK for this specific issue: pandas doesn't do anything to open or close your database connections. That's left up to the user / underlying object. A link to the sqlalchemy docs on this subject should be included.

@emattiza
Copy link

I'll take a shot at getting this one documented!

@cruzzoe
Copy link
Contributor

cruzzoe commented Nov 2, 2019

#29363

@felipedmc
Copy link

felipedmc commented Jan 10, 2023

Hey there! Some years later... no explict engine, conection or cursor, and my code goes something like:

lazydummy = pandas.read_sql(sql= 'SELECT * FROM somewhere' , con= 'postgre://blablabla')
.
. #doing stuff with my dataframe
.
display(lazydummy.head() #BREAKPOINT HERE!
END OF SCRIPT

At the break point, I alt+tab to my pgadmin 4 and the conection was there, opened. Back to my phyton code, finished running it, gone back to pgadmin and the conection was not there anymore on my db dashboard. Guess it just autocloses when the script is over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs good first issue IO SQL to_sql, read_sql, read_sql_query
Projects
None yet
8 participants