How to preserve dtype int when reading integers with NaN in pandas

Use dtype 'Int64' for NaN support

  • 'Int64' (capital I) is a pandas nullable integer, so it can mix with NaNs.
  • Default numpy integers cannot mix with NaNs, so the column will become dtype object.

For example, say column Col D contains only integers and NaNs:

  • Either use the dtype param at load time (available in most read_* methods):

    df = pd.read_sql_query(sql_script.read(), engine, dtype={'Col D': 'Int64'})
    #                                                 ^^^^^
    
  • Or use astype after loading:

    df = pd.read_sql_query(sql_script.read(), engine).astype({'Col D': 'Int64'})
    #                                                 ^^^^^^