1

I have a pandas df like this:

      A         B       C             D            E       F        G        H
0  1033548.0   0.0  20190324.0   2019-04-13       NaT   10176.0    NaN  40360001.0
1  1033549.0   0.0  20190324.0   2019-04-13 2019-04-13   10182.0    NaN  41360562.0
2  1033550.0   0.0  20190324.0   2019-04-13       NaT   10433.0    NaN  45180156.0
3  1033551.0   0.0  20190324.0   2019-04-13       NaT   10019.0    123         NaN
4  1033552.0   0.0  20190324.0   2019-04-13       NaT   10333.0    NaN  40120165.0
5  1033553.0   0.0  20190324.0   2019-04-13       NaT   10246.0    NaN  40820596.0 
6  1033554.0   0.0  20190324.0          NaT       NaT   10107.0    NaN         NaN
7  1033555.0   0.0  20190324.0   2019-04-13       NaT   10131.0    NaN  41190549.0
8  1033556.0   0.0  20190324.0   2019-04-13       NaT   10057.0    NaN  40430581.0
9  1033557.0   0.0  20190324.0   2019-04-13       NaT   10009.0    NaN  40360012.0 

I want to write this df to my Oracle Database Table using SQL Insert Statement. But since my df contains NaNs and NaTs, so its giving me error: cx_Oracle.DatabaseError: DPI-1043: invalid number. How can I write this df to the oracle table using SQL which will not give me any error? Any help would be appreciated.

Prachi
  • 494
  • 3
  • 8
  • 21

3 Answers3

1

Prachi.

Your column G is of type number in Oracle, right? Thus, I believe you need to set your NaNs with 0 (zeros), if possible.

Or make 2 types of inserts:

  1. With column G, if it is exists on DF;
  2. Without column G, if it doesn't exists on DF.
GRCorso
  • 11
  • 1
0

You have to replace your {float} nan by None in the binding list.

import math

bind_insert = lines_to_insert_df.values.tolist()

for b in bind_insert :

    for index, value in enumerate(b):
        if isinstance(value, float) and math.isnan(value):
            b[index] = None
        elif isinstance(value, type(pd.NaT)):
            b[index] = None

con.cursor().executemany(query, bind_insert)
khalySeb
  • 1
  • 1
0

I had the same issue. I do not think it is a NaN issue since it works ok for me with NaN values.

In my case there was a float column that had inf values due to divide by 0. Once I took care of this, the write to Oracle issue went away.

Nikhil Gupta
  • 1,436
  • 12
  • 15