I am trying to export a Pandas DataFrame to an Oracle database. I have come across the Write_Frame function in Pandas which sounds like exactly what I need.
However, I have done tons of searches online and just can't get it to work. I have imported cx_Oracle and can connect to the Oracle database as well as running SQL queries without any problems, but when I run this it gives me a 'NotImplementedError':
import pandas.io.sql as psql
output = psql.write_frame(MyResults, name = 'MySchema.MyTable', con = MyCon,
flavor = 'oracle', if_exists = 'replace')
So far I have seen many examples of wrtie_frame on sqlite and mysql, so does that mean it won't work if flavor = 'oracle'? I have tried changing the flavor to mysql but it gives me an error saying 'Invalid SQL statement'??
Could anyone help me fixing this as I prefer not having to write the results to a CSV file then export to the database table?
Thanks
-
What version of pandas are you? Open up the python shell and type in "import pandas", then "pandas.__version__"Will– Will2014年03月17日 17:49:19 +00:00Commented Mar 17, 2014 at 17:49
-
You could read directly into python, but obviously there is potentially a lot of overhead in that. You could try doing this on pandas' master branch (which uses SQLAlchemy under the hood), in fact it would be great to get your feedback on that and ensure it's working in 0.14!Andy Hayden– Andy Hayden2014年03月17日 18:10:47 +00:00Commented Mar 17, 2014 at 18:10
1 Answer 1
To expand on Andy Hayden's comment: oracle is indeed not supported in pandas <= 0.13 and older (only sqlite and mysql were supported).
But the sql module got a big overhaul in 0.14 (in development at the moment). It now uses sqlalchemy under the hood, so normally oracle should now be supported through sqlalchemy. See the dev docs for more details: http://pandas-docs.github.io/pandas-docs-travis/io.html#io-sql. And indeed, if you would be able to test it with oracle and give some feedback, that would be fantastic!
4 Comments
engine = create_engine('oracle+cx_oracle://User:Passowrd@mytns'). However, I have no luck after that as I tried writing to the database using to_sql() and it still doesn't work : df.to_sql('TRORE.HW_TEST_PY',engine, flavor = 'oracle, if_exists = 'replace') and not engine.has_table('MY_TABLE','MY_SCHEMA') either. So is it the case that you can create an engine in 0.13 but if you want to do anything with it you need 0.14?create_engine is just an sqlalchemy function, so that also works in 0.13, but pandas to_sql only uses it from 0.14. If you can't upgrade to the pandas development version, another option is to download just the file with the sql code (github.com/pydata/pandas/blob/master/pandas/io/sql.py), save this next to your code, import it (import sql) and then you can use the new functionality as sql.to_sql(df, 'name', engine)Explore related questions
See similar questions with these tags.