From 61a31fffb77fcedc2c461459f08f80501e8941ee Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Wed, 2 Mar 2022 15:00:49 +0100 Subject: [PATCH] FIX: Don't cast float to int. --- src/caosadvancedtools/table_importer.py | 11 +++++------ unittests/test_table_importer.py | 2 -- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/caosadvancedtools/table_importer.py b/src/caosadvancedtools/table_importer.py index 2a5df209..1f515e78 100755 --- a/src/caosadvancedtools/table_importer.py +++ b/src/caosadvancedtools/table_importer.py @@ -312,8 +312,8 @@ class TableImporter(): .. note:: - If columns are float, but should be integer or vice versa, this method converts the - respective columns in place. + If columns are integer, but should be float, this method converts the respective columns + in place. Parameters ---------- @@ -323,14 +323,13 @@ class TableImporter(): """ for key, datatype in self.datatypes.items(): - # Check for castable numeric types first: We unconditionally cast float to int and vice - # versa, because CaosDB does not have different sizes anyway. + # Check for castable numeric types first: We unconditionally cast int to the default + # float, because CaosDB does not have different sizes anyway. col_dtype = df.dtypes[key] if not strict and not np.issubdtype(col_dtype, datatype): issub = np.issubdtype # These special cases should be fine. - if ((issub(col_dtype, np.integer) and issub(datatype, np.floating)) - or (issub(col_dtype, np.floating) and issub(datatype, np.integer))): + if issub(col_dtype, np.integer) and issub(datatype, np.floating): df[key] = df[key].astype(datatype) # Now check each element diff --git a/unittests/test_table_importer.py b/unittests/test_table_importer.py index f2727472..4c7d044e 100644 --- a/unittests/test_table_importer.py +++ b/unittests/test_table_importer.py @@ -218,12 +218,10 @@ class XLSImporterTest(TableImporterTest): "float_as_float": float, "int_as_float": float, "int_as_int": int, - "float_as_int": int, } ) df = importer.read_xls(os.path.join(os.path.dirname(__file__), "data", "datatypes.xlsx")) assert np.issubdtype(df.loc[0, "int_as_float"], float) - assert df.loc[1, "float_as_int"] == 6 # This is an acceptable rounding error. class CSVImporterTest(TableImporterTest): -- GitLab