Skip to content

Commit

Permalink
src: convert pandas NaN values to None
Browse files Browse the repository at this point in the history
  • Loading branch information
henrich14 committed Oct 31, 2024
1 parent ceddc3b commit 4bfbb3c
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/pyBADA/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def list_subfolders(folderPath):
@staticmethod
def safe_get(df, column_name, default_value=None):
"""
Safely retrieves a column's value from a DataFrame, returning a default value if the column does not exist.
Safely retrieves a column's value from a DataFrame, returning a default value if the column does not exist or if the value is NaN.
:param df: DataFrame to retrieve the value from.
:param column_name: Name of the column to retrieve.
Expand All @@ -53,7 +53,18 @@ def safe_get(df, column_name, default_value=None):
"""

if column_name in df.columns:
return df[column_name].iloc[0]
value = df[column_name].iloc[0]

if isinstance(value, list):
if pd.isna(value).all():
return default_value
else:
return value
else:
if pd.isna(value):
return default_value
else:
return value
else:
return default_value

Expand Down

0 comments on commit 4bfbb3c

Please sign in to comment.