Problem loading a function in Python to_sql - python

I have the following code in two scripts called Playing_DB_Around.py with the following code:
import data_base.Calc_Func as calc
import pandas as pd
df = pd.DataFrame({'Name': ['John', 'Jane', 'Jim'], 'Age': [25, 30, 35]})
db = calc.Database("example_db")
calc.Database.to_sql(df, "example_table")
This code loads a own written bib which is in the script Calc_Func.py where the code is:
from sqlalchemy import create_engine
class Database:
def __init__(self, db_file):
self.engine = create_engine(f'sqlite:///{db_file}')
def to_sql(self, df, table_name):
df.to_sql(table_name, self.engine, if_exists='replace', index=False)
When executing Playing_DB_Around.py I receive the following error message. I am confused the class and the execution in one script it seems to work. I tried many things but somehow cant get it to run.
Error message.
Traceback (most recent call last): File "Playing_DB_Around.py", line
9, in
calc.Database.to_sql(df, "example_table") TypeError: to_sql() missing 1 required positional argument: 'table_name'

Try this:
db.to_sql(df, "example_table")
seems like db is the instance and not the calc.Database

Related

read_sql_query() throws "'OptionEngine' object has no attribute 'execute'" with SQLAlchemy 2.0.0

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
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)
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))
Solution#01:
Open cmd or terminal and Try with
pip install --user --upgrade "sqlalchemy<2.0"
Must restart your kernel

Using one module inside another in Python

I am trying to make some code a bit more modular in Python and am running into one issue which I'm sure is straight forward but I can't seem to see what the problem is.
Suppose I have a script, say MyScript.py:
import pandas as pd
import myFunction as mF
data_frame = mF.data_imp()
print(data_frame)
where myFunction.py contains the following:
def data_imp():
return pd.read_table('myFile.txt', header = None, names = ['column'])
Running MyScript.py in the command line yields the following error:
Traceback (most recent call last):
File "MyScript.py", line 5, in <module>
data_frame = mF.data_imp()
File "/Users/tomack/Documents/python/StackQpd/myFunction.py", line 2, in data_imp
return pd.read_table('myFile.txt', header = None, names = ['column'])
NameError: name 'pd' is not defined
You need to import pandas in your function or script myFunction:
def data_imp():
import pandas as pd
return pd.read_table('myFile.txt', header = None, names = ['column'])
Answers here are right, because your module indeed lacks myFunction import.
If stated more broad this question can also contain following: in case of circular import the only 2 remedies are:
import pandas as pd , but not from pandas import something
Use imports right there in functions you need in-placd

Python: Function: not recognizing dependencies

I am quite new to writing modules in Python.
I use Python 3.5
I have a script called describeToolbox.py that contain functions that I would like to be able to call, like this one:
#describeToolbox.py
import shelve
def getRawData(prefix):
shelfFile = shelve.open('data'+prefix)
df = shelfFile['data'+prefix]
shelfFile.close()
return df
This is meant to retrieve a dataFrame from a shelve file
In my console now, I write the following statements:
In [7]:import shelve
import describeToolbox as TB
In [8]:TB.getRawData('Myprefix')
Out [8]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-67160af666cc> in <module>()
----> 1 TB.getRawData('Myprefix')
C:\Users\Math\Documents\Docs\Commos\Notebooks\describeToolbox.py in getRawData(prefix)
1 def getRawData(prefix):
----> 2 shelfFile = shelve.open('data'+prefix)
3 df = shelfFile['data'+prefix]
4 shelfFile.close()
5 return df
NameError: name 'shelve' is not defined
It gives me an error message saying that the module 'shelve', the dependency, is not defined.
Basically I dont know where is the correct place to import all the dependencies so that my function can load them when I want to import it.
I would like to write a depository of functions I use often in one module and call them when needed.
Thank you for your help!

Import .json to Database

I successfully downloaded tweets into a json file. Now I try to import it in a database with this function:
def import_json(fi):
logging.warning("Loading tweets from json file {0}".format(fi))
for line in open(fi, "rb"):
data = json.loads(line.decode('utf-8'))
database.create_tweet_from_dict(data)
the json-file "keywords_BVBS04.json" lays in a folder called data which is in the current directory. The function is in a file called BVBS04.py
to start the import I type BVBS04.import_json(keywords_BVBS04.json) in ipython in the console. this is what I get back:
NameError Traceback (most recent call
last) in ()
----> 1 BVBS04.import_json(keywords_BVBS04.json)
NameError: name 'keywords_BVBS04' is not defined
Now here comes the is a beginner's question: Where/how do I have to define "keywords_BVBS04"? I tried a lot :(
Thanks!
This is what you want,
1) you need to import the function from the script, not use dot-notation on the script.
2) Quote the filename.
>>> from BVBS04 import import_json
>>> import_json("keywords_BVBS04.json")
Good luck with the rest of things

Writing and importing custom modules/classes

I've got a class that I'm trying to write called dbObject and I'm trying to import it from a script in a different folder. My structure is as follows:
/var/www/html/py/testobj.py
/var/www/html/py/obj/dbObject.py
/var/www/html/py/obj/__init__.py
Now, __init__.py is an empty file. Here are the contents of dbObject.py:
class dbObject:
def __init__():
print "Constructor?"
def test():
print "Testing"
And here's the contents of testobj.py:
#!/usr/bin/python
import sys
sys.path.append("/var/www/html/py")
import obj.dbObject
db = dbObject()
When I run this, I get:
Traceback (most recent call last):
File "testobj.py", line 7, in <module>
db = dbObject()
NameError: name 'dbObject' is not defined
I'm new to Python, so I'm very confused as to what I'm doing wrong. Could someone please point me in the right direction?
EDIT: Thanks to Martijn Pieters' answer I modified my testobj.py as follows:
#!/usr/bin/python
import sys
sys.path.append("/var/www/html/py")
sys.path.append("/var/www/html/py/dev")
from obj.dbObject import dbObject
db = dbObject()
However, now when I run it I get this error:
Traceback (most recent call last):
File "testobj.py", line 7, in <module>
db = dbObject()
TypeError: __init__() takes no arguments (1 given)
Is this referring to my init.py or the constructor within dbObject?
EDIT(2): Solved that one myself, the constructor must be able to take at least one parameter - a reference to itself. Simple fix. Looks like this problem is solved!
EDIT (Final): This is nice - I can cut out the import sys and sys.path.append lines and it still works in this instance. Lovely.
You need to import the class from the module:
from obj.dbObject import dbObject
This adds the class dbObject directly to your local namespace.
Your statement import obj.dbObject adds the name obj to the local namespace, so you could also do this instead:
db = obj.dbObject.dbObject()
because obj.dbObject is the dbObject.py module in your obj package directory.

Categories

Resources