Skip to content

Commit

Permalink
Add is_none/nan datatype tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ejohb committed Nov 22, 2024
1 parent c5c97cd commit 87c43fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions fmtr/tools/datatype_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,36 @@ def to_bool(raw: Any, default=None) -> bool:
return default


def is_nan(value: Any) -> bool:
"""
Import-tolerant test if nan
"""
try:
import pandas as pd
return pd.isna(value)
except ImportError:
return False

def is_none(value: Any) -> bool:
"""
Test if none - or nan
"""
if is_nan(value):
return True

return value is None


def none_else(value: Any, default: Any) -> Any:
"""
Ternary "if none else" function
"""
if is_none(value):
return default
return value
2 changes: 1 addition & 1 deletion fmtr/tools/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.3
1.0.4

0 comments on commit 87c43fc

Please sign in to comment.