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

DOC: example in docstring of to_sql is out of date #51105

Closed
1 task done
vahtras opened this issue Feb 1, 2023 · 2 comments
Closed
1 task done

DOC: example in docstring of to_sql is out of date #51105

vahtras opened this issue Feb 1, 2023 · 2 comments
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@vahtras
Copy link

vahtras commented Feb 1, 2023

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_sql.html

Documentation problem

Everything is fine with the pinned dependencies in the pandas distribution (environment.yml)

However, when I in another context start with a fresh environment with later versions of python and sqlalchemy the examples in the docstring do not work. In particular with docstring to_sql

>>> import pandas as pd
>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite://', echo=False)
>>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
>>> df
     name
0  User 1
1  User 2
2  User 3
>>> df.to_sql('users', con=engine)
3
>>> engine.execute("SELECT * FROM users").fetchall()
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3')]

a doctest gives

$ python -m doctest /tmp/from_to_sql_docstring.md                         1 ↵
**********************************************************************
File "/tmp/from_to_sql_docstring.md", line 15, in from_to_sql_docstring.md
Failed example:
    engine.execute("SELECT * FROM users").fetchall()
Exception raised:
    Traceback (most recent call last):
      File "/home/olav/.pyenv/versions/3.10.5/lib/python3.10/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest from_to_sql_docstring.md[6]>", line 1, in <module>
        engine.execute("SELECT * FROM users").fetchall()
    AttributeError: 'Engine' object has no attribute 'execute'
**********************************************************************
1 items had failures:
   1 of   7 in from_to_sql_docstring.md
***Test Failed*** 1 failures.

I am here using versions python3.10.5 and versions
pandas==1.5.3
sqlalchemy==2.0.0

Suggested fix for documentation

The smallest fix I can think of that works in both versions is

>>> import pandas as pd
>>> from sqlalchemy import create_engine, text
>>> engine = create_engine('sqlite://', echo=False)
>>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
>>> df
     name
0  User 1
1  User 2
2  User 3
>>> df.to_sql('users', con=engine)
3
>>> with engine.connect() as conn:
...    conn.execute(text("SELECT * FROM users")).fetchall()
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3')]

of course, there could be a better way

@vahtras vahtras added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 1, 2023
@phofl
Copy link
Member

phofl commented Feb 1, 2023

Thanks for your report, duplicate of #51015

@phofl phofl closed this as completed Feb 1, 2023
@phofl
Copy link
Member

phofl commented Feb 1, 2023

Can you post in #40686

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants