I am trying to use pandas to select some data from an Oracle database. The column in question has the data type TIMESTAMP(6) WITH TIME ZONE
. I am in the same time zone as the database, but it contains data that is recorded from a different time zone.
Oracle version: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
Python 3.8.13
SQLAlchemy 1.4.39
cx_Oracle 8.3.0
In PL/SQL Developer, the query works:
SELECT col
FROM table
Returns
18-JAN-21 09.54.58.000000000 PM ASIA/BANGKOK
In Python, I get this error:
import sqlalchemy
import cx_Oracle
server = server
port = port
sid = sid
username = username
password = password
dsn_tns = cx_Oracle.makedsn(server, port, sid)
cnxn = cx_oracle.connect(username, password, dsn_tns)
query = """
SELECT col
FROM table
"""
df = pd.read_sql_query(query, cnxn)
Output:
DatabaseError: ORA-01805: possible error in date/time operation
After some SO searching, I tried this:
query = """
SELECT CAST(TO_TIMESTAMP_TZ(
col,
'DD-MMM-YY HH.MI.SS.FF6 TZH TZR')
) AT TIME ZONE 'ASIA/BANGKOK' AS col
FROM table
"""
df = pd.read_sql_query(query, cnxn_tds_dev)
Which returns a different error message:
ORA-00905: missing keyword
How can I just select this timestamp column (and several others) using Python/SQLAlchemy/cx_Oracle? Because the query works in PL/SQL Developer, I am assuming it is an issue with cx_Oracle. I will try creating a new Python environment with an older version of cx_Oracle, per this post.