53

First of all, I'm a totally new guys in the dev world I'm currently taking courses in AI / Data Science and one of my work is to use a SQL Database to make prediction using Prophet, then use these predition to make a PowerBI But currently, I'm stuck with the Python code, I'm not a developer initially, so I have no clue where the problem is:

import sqlalchemy
from sqlalchemy import create_engine
import pandas as pd
from prophet import Prophet
import pymysql


engine = create_engine("mysql+pymysql://root:Password@localhost:3306/data")
query = "SELECT Cle_Produit, Date_Facturation, SUM(Quantite) AS Total_Quantite FROM ventes GROUP BY         Cle_Produit, Date_Facturation"
df = pd.read_sql_query(query, engine)

df = df.pivot(index='Date_Facturation', columns='Cle_Produit', values='Total_Quantite')
df = df.reset_index()
df.rename(columns={'Date_Facturation': 'ds', 'Total_Quantite': 'y'}, inplace=True)


m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=365)
forecast = m.predict(future)

forecast[['ds', 'yhat']].to_csv('forecast.csv', index=False)

It returns me this message:

Importing plotly failed. Interactive plots will not work. Traceback (most recent call last): File "f:\Backup\Cours\Cours\Explo Data\app3.py", line 9, in df = pd.read_sql_query(query, engine) File "F:\Programmes\Anaconda\envs\myenv\lib\site-packages\pandas\io\sql.py", line 397, in read_sql_query return pandas_sql.read_query( File "F:\Programmes\Anaconda\envs\myenv\lib\site-packages\pandas\io\sql.py", line 1560, in read_query result = self.execute(*args) File "F:\Programmes\Anaconda\envs\myenv\lib\site-packages\pandas\io\sql.py", line 1405, in execute return self.connectable.execution_options().execute(*args, **kwargs) AttributeError: 'OptionEngine' object has no attribute 'execute'

Please, can somebody help me?

I want this python script to create a csv file with the prediction from prophet. I want Prophet to use the table ventes from the DB data, and it should use the column Cle_Produit, Quantite and Date_Facturation

8 Answers 8

59

The latest version of SQLAlchemy (2.0) has removed Engine.execute. For the time being you may need to downgrade SQLAlchemy

python -m pip install --upgrade 'sqlalchemy<2.0'

(or the equivalent conda commands if you use conda).

Or, as Gord Thompson points out in his comment, wrap the query with sqlalchemy.text.

from sqlalchemy import text

# ...

with engine.begin() as conn:
    query = text("""SELECT * FROM tbl""")
    df = pd.read_sql_query(query, conn)

Or see InnocentBystander's answer for a workaround.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for you help ! I tried your solution, both, but I still have the same problem. I forget to say, I work on Visual Studio Code with Anaconda Env, and I tried it also in a jupyter notebook and VSC notebook, nothing worked
Unfortunately I use neither VSC nor Jupyter. But probably you have to find the env in VSC and ensure that the packages are installed there (and probably using conda, not pip).
pandas 1.5.3 does not work for me with SQLA 2.0.0. If I try df = pd.read_sql_query("SELECT 1 AS foo", engine) I get "AttributeError: 'OptionEngine' object has no attribute 'execute'". If I try with engine.begin() as conn: followed by df = pd.read_sql_query("SELECT 1 AS foo", conn) I get "sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: 'SELECT 1 AS foo'"
@GordThompson rats, I thought 1.5.3 had the 2.0 fixes, but the release notes just say they are "working towards" compatibility. Thanks for checking
However, df = pd.read_sql_query(sa.text("SELECT 1 AS foo"), conn) does work.
|
33

Here a similar problem is discussed.

The syntax that works for sqlAlchemy 2.0 and that is consistent with the prior api is.

    from sqlalchemy import create_engine, text as sql_text
    connection = create_engine(bla)
    query = "blo"
    df = pandas.read_sql_query(con=connection.connect(), 
                                  sql=sql_text(query))

3 Comments

Thank you very much, works also fine with geopandas.GeoDataFrame.from_postgis.
This answer is preferable since it doesn't include downgrading of any packages. Long-term perspective.
seems you also have to explicitly call .connect() as well
15

For pandas read_sql_query, there are two things that are easy to get wrong. To avoid this error you need to pass a connection (not the engine) and you need to use the text function to convert the query.

import pandas as pd
from sqlalchemy import create_engine, text
engine = create_engine("mysql+mysqldb://usr:pwd@localhost/dbs")
conn = engine.connect()

query = text('SELECT * FROM table')
df = pd.read_sql_query(query, conn)

Replace "usr", "pwd", "dbs "and "table" with your own strings. You can also read the whole table with pandas read_sql_table without using text. Setup as above:

df = pd.read_sql_table('table', conn)

Some links into the sqlalchemy documentation for more about engines and text:

https://docs.sqlalchemy.org/en/20/core/engines_connections.html

https://docs.sqlalchemy.org/en/20/core/sqlelement.html#sqlalchemy.sql.expression.text

1 Comment

This helped me as I was not using the text method on the string based sql query
4

This issue has now fixed in pandas 2.0.0 with the pull request(Make pandas/io/sql.py work with sqlalchemy 2.0).

1 Comment

... which breaks other things, including py37 support:)
2

For anyone who comes across this, the git issue for reference:

https://github.com/pandas-dev/pandas/issues/51015

Comments

1

I was also getting the same error. My sqlalchemy version is 2.0.5.post1. So I have just replaced the engine with engine.connect().

Your code:

Before:

engine = create_engine("mysql+pymysql://root:Password@localhost:3306/data")
query = "SELECT Cle_Produit, Date_Facturation, SUM(Quantite) AS Total_Quantite FROM ventes GROUP BY Cle_Produit, Date_Facturation"
df = pd.read_sql_query(query, engine)

After:

engine = create_engine("mysql+pymysql://root:Password@localhost:3306/data")
query = "SELECT Cle_Produit, Date_Facturation, SUM(Quantite) AS Total_Quantite FROM ventes GROUP BY Cle_Produit, Date_Facturation"
df = pd.read_sql_query(query, engine.connect())

Comments

0

in my case, switching to session.connection() fixed the issue

pd.read_sql(query, db_session.connection())

Comments

-1

Solution#01: Open cmd or terminal and Try with

pip install --user --upgrade "sqlalchemy<2.0"

Must restart your kernel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.