From 24607ffab34de261942c1ecb52f70d1d08f52362 Mon Sep 17 00:00:00 2001 From: Stephane Struzik <34556337+sstruzik@users.noreply.github.com> Date: Thu, 16 May 2024 14:10:03 +0100 Subject: [PATCH] fix issue when categorical column have a default value (#115) * fix issue when categorical column have a default value --- ods_tools/oed/source.py | 5 ++++- tests/test_ods_package.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ods_tools/oed/source.py b/ods_tools/oed/source.py index 6100a49e..f891797f 100644 --- a/ods_tools/oed/source.py +++ b/ods_tools/oed/source.py @@ -305,7 +305,10 @@ def prepare_df(cls, df, column_to_field, ods_fields): if (field_info and field_info['Default'] != 'n/a' and (df[col].isna().any() or (field_info['pd_dtype'] == 'category' and df[col].isnull().any()))): - fill_empty(df, col, df[col].dtype.type(field_info['Default'])) + if df[col].dtype.name == 'category': + fill_empty(df, col, field_info['Default']) + else: + fill_empty(df, col, df[col].dtype.type(field_info['Default'])) elif df[col].dtype.name == 'category': fill_empty(df, col, '') diff --git a/tests/test_ods_package.py b/tests/test_ods_package.py index 14f9f094..189cbe62 100644 --- a/tests/test_ods_package.py +++ b/tests/test_ods_package.py @@ -111,6 +111,24 @@ def test_load_oed_from_config(self): exposure2 = OedExposure(**config) self.assertTrue(exposure.location.dataframe.equals(exposure2.location.dataframe)) + def test_categorical_with_default(self): + # UseReinsDates is a string column with a non null default, check default setting works + with tempfile.TemporaryDirectory() as tmp_run_dir: + config = { + 'ri_info': base_url + '/SourceReinsInfoOEDPiWind.csv', + 'use_field': True + } + exposure = OedExposure(**config) + exposure.ri_info.dataframe['UseReinsDates'] = None + exposure.ri_info.dataframe.to_csv(os.path.join(tmp_run_dir, 'ri_info.csv'), index=False) + + exposure = OedExposure(**{ + 'ri_info': os.path.join(tmp_run_dir, 'ri_info.csv'), + 'use_field': True + }) + ri_scope = exposure.ri_info.dataframe + self.assertTrue(isinstance(ri_scope, pd.DataFrame)) + def test_load_oed_from_df(self): location_df = pd.DataFrame({ 'PortNumber': [1, 1],