Skip to content
Snippets Groups Projects
Verified Commit 61a31fff authored by Daniel Hornung's avatar Daniel Hornung
Browse files

FIX: Don't cast float to int.

parent d07196be
No related branches found
No related tags found
2 merge requests!39Release 0.4.0,!34TableConverter now converts int to float and vice versa to match the desired dtype.
Pipeline #19891 failed
This commit is part of merge request !34. Comments created here will be created in the context of that merge request.
...@@ -312,8 +312,8 @@ class TableImporter(): ...@@ -312,8 +312,8 @@ class TableImporter():
.. note:: .. note::
If columns are float, but should be integer or vice versa, this method converts the If columns are integer, but should be float, this method converts the respective columns
respective columns in place. in place.
Parameters Parameters
---------- ----------
...@@ -323,14 +323,13 @@ class TableImporter(): ...@@ -323,14 +323,13 @@ class TableImporter():
""" """
for key, datatype in self.datatypes.items(): for key, datatype in self.datatypes.items():
# Check for castable numeric types first: We unconditionally cast float to int and vice # Check for castable numeric types first: We unconditionally cast int to the default
# versa, because CaosDB does not have different sizes anyway. # float, because CaosDB does not have different sizes anyway.
col_dtype = df.dtypes[key] col_dtype = df.dtypes[key]
if not strict and not np.issubdtype(col_dtype, datatype): if not strict and not np.issubdtype(col_dtype, datatype):
issub = np.issubdtype issub = np.issubdtype
# These special cases should be fine. # These special cases should be fine.
if ((issub(col_dtype, np.integer) and issub(datatype, np.floating)) if issub(col_dtype, np.integer) and issub(datatype, np.floating):
or (issub(col_dtype, np.floating) and issub(datatype, np.integer))):
df[key] = df[key].astype(datatype) df[key] = df[key].astype(datatype)
# Now check each element # Now check each element
......
...@@ -218,12 +218,10 @@ class XLSImporterTest(TableImporterTest): ...@@ -218,12 +218,10 @@ class XLSImporterTest(TableImporterTest):
"float_as_float": float, "float_as_float": float,
"int_as_float": float, "int_as_float": float,
"int_as_int": int, "int_as_int": int,
"float_as_int": int,
} }
) )
df = importer.read_xls(os.path.join(os.path.dirname(__file__), "data", "datatypes.xlsx")) 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 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): class CSVImporterTest(TableImporterTest):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment