Skip to content

Commit

Permalink
fix issue when categorical column have a default value (#115)
Browse files Browse the repository at this point in the history
* fix issue when categorical column have a default value
  • Loading branch information
sstruzik authored May 16, 2024
1 parent 6ced5ca commit 24607ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ods_tools/oed/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')

Expand Down
18 changes: 18 additions & 0 deletions tests/test_ods_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 24607ff

Please sign in to comment.