2

I'm trying to use Pandas read_sql to validate some fields in my app.

When i read my db using SQL Developer, i get these values:

  1. 603.29
  2. 1512.00
  3. 488.61
  4. 488.61

But reading the same sql query using Pandas, the decimal places are ignored and added to the whole-number part. So i end up getting these values:

  1. 60329.0
  2. 1512.0
  3. 48861.0
  4. 48861.0

How can i fix it?

2
  • You could try adding coerce_float=Flase in read_sql() Commented Oct 30, 2019 at 14:54
  • That just removed the '.0' but didn't fixed the real problem. I've changed the sql query to convert that column to char for now. Commented Oct 30, 2019 at 17:55

2 Answers 2

3

I've found a workaround for now.

Convert the column you want to string, then after you use Pandas you can convert the string to whatever type you want.

Even though this works, it doesn't feel right to do so.

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

Comments

0

Could you specify what SQL you are using? I just encountered a bit similar problem, which I overcame by defining the datatype more specifically in the query - here the solution for it. I guess you could use the similar approach and see if it works.

In my MySQL database I have four columns with very high precision Screenshot of SQL table

When I tried to read them with this query, they were truncated to 5 digits after the decimal delimiter.

    query = """select  
               y_coef,
               y_intercept,
               x_coef,
               x_intercept
               from TABLE_NAME"""
    df = pd.read_sql(query, connection)

However, when I specified that I want to have them with the precision of 15 digits after the decimal delimiter like below, they were not truncated anymore.

        query = """select 
               cast(y_coef as decimal(15, 15)) as y_coef,
               cast(y_intercept as decimal(15, 15)) as y_intercept,
               cast(x_coef as decimal(15, 15)) as x_coef,
               cast(x_intercept as decimal(15, 15)) as x_intercept
               from TABLE_NAME"""
    df = pd.read_sql(query, connection)

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.