Skip to content

Default type map float64 -> float is wrong #212

Open
@shraik

Description

@shraik

When saving a pandas frame with the dtype "float64" the wrong type is substituted.
It is more correct to substitute the type cratedb:double or sqlalchemy:DECIMAL

import sqlalchemy as sa
import pandas as pd

data = {
    "col_1": [19556.88, 629414.27, 51570.0, 2933.52, 20338.98],
    "col_2": [
        15379.920000000002,
        1107140.42,
        8081.999999999999,
        1570.0300000000002,
        29468.539999999997,
    ],
}
df_data = pd.DataFrame.from_dict(data)
print(df_data.dtypes)

dburi = "crate://panduser:[email protected]:4200?ssl=false"
engine = sa.create_engine(dburi, echo=False)
conn = engine.connect()

df_data.to_sql(
    "test_sum",
    conn,
    if_exists="replace",
    index=False,
)
conn.exec_driver_sql("REFRESH TABLE test_sum;")
df_load = pd.read_sql_table("test_sum", conn)

pd.options.display.float_format = "{:.12f}".format
print(df_data.sort_values(by="col_1"))
print("dataframe after loading")
print(df_load.sort_values(by="col_1"))

output:

col_1    float64
col_2    float64
dtype: object
                col_1                col_2
3   2933.520000000000    1570.030000000000
0  19556.880000000001   15379.920000000002
4  20338.980000000000   29468.539999999997
2  51570.000000000000    8081.999999999999
1 629414.270000000019 1107140.419999999925
dataframe after loading
                col_1                col_2
0   2933.520000000000    1570.030000000000
1  19556.880000000001   15379.920000000000
4  20338.980000000000   29468.540000000001
3  51570.000000000000    8082.000000000000
2 629414.250000000000 1107140.399999999907

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions