Search code examples
pythonmysqlpandasinteger

Pandas read sql integer became float


When I use pandas to read a MySQL table, some columns (see 'to_nlc' in the image below) that used to have an integer type became a floating-point number (automatically append .0).

Can anyone figure out why this happens? Or at least have a guess?

enter image description here enter image description hereenter image description here


Solution

  • Problem is your data contains NaN values, so int is automatically cast to float.

    I think you can check NA type promotions:

    When introducing NAs into an existing Series or DataFrame via reindex or some other means, boolean and integer types will be promoted to a different dtype in order to store the NAs. These are summarized by this table:

    Typeclass   Promotion dtype for storing NAs
    floating    no change
    object      no change
    integer     cast to float64
    boolean     cast to object
    

    While this may seem like a heavy trade-off, in practice I have found very few cases where this is an issue in practice. Some explanation for the motivation here in the next section.