From 845b4e60ac95bce86d150deb4f61103a39c0505f Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Mon, 4 Jul 2022 17:08:20 +0100 Subject: [PATCH] fix: pandas.to_sql does not write an index to the db anymore, docs extended, test added. --- pyerrors/input/pandas.py | 6 +++--- tests/pandas_test.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pyerrors/input/pandas.py b/pyerrors/input/pandas.py index 9c32b3ba..1488dac0 100644 --- a/pyerrors/input/pandas.py +++ b/pyerrors/input/pandas.py @@ -8,7 +8,7 @@ from .json import create_json_string, import_json_string def to_sql(df, table_name, db, if_exists="replace", gz=True): - """Write DataFrame inlcuding Obs or Corr valued columns to sqlite database. + """Write DataFrame including Obs or Corr valued columns to sqlite database. Parameters ---------- @@ -25,12 +25,12 @@ def to_sql(df, table_name, db, if_exists="replace", gz=True): """ se_df = _serialize_df(df, gz=gz) con = sqlite3.connect(db) - se_df.to_sql(table_name, con, if_exists=if_exists) + se_df.to_sql(table_name, con, if_exists=if_exists, index=False) con.close() def read_sql_query(sql, db, auto_gamma=False): - """Write DataFrame inlcuding Obs or Corr valued columns to sqlite database. + """Execute SQL query on sqlite database and obatin DataFrame including Obs or Corr valued columns. Parameters ---------- diff --git a/tests/pandas_test.py b/tests/pandas_test.py index 059010c9..4d34d4e0 100644 --- a/tests/pandas_test.py +++ b/tests/pandas_test.py @@ -51,3 +51,13 @@ def test_gz_serialization(): ser = pe.input.pandas._serialize_df(my_df, gz=gz) deser = pe.input.pandas._deserialize_df(ser) np.all(my_df == deser) + + +def test_sql(tmp_path): + my_list = [{"Label": i, "Obs": pe.pseudo_Obs(5 * np.exp(-0.2 * i), 0.01, "test_ensemble", 20)} for i in range(150)] + pe_df = pd.DataFrame(my_list) + my_db = (tmp_path / "test_db.sqlite").as_posix() + pe.input.pandas.to_sql(pe_df, "My_table", my_db) + for auto_gamma in [False, True]: + re_df = pe.input.pandas.read_sql_query("SELECT * from My_table", my_db, auto_gamma=auto_gamma) + assert np.all(re_df == pe_df)