Merged in feature/fix_string_utils (pull request #482)
Enhance is_empty function to handle pandas Series and improve empty value checks * Enhance is_empty function to handle pandas Series and improve empty value checks * Enhance is_empty function to support pandas Series and add pd_mask parameter for flexible empty checks Approved-by: Katon Minhas
This commit is contained in:
@@ -284,23 +284,32 @@ def count_reimbursements_in_exhibit(exhibit_text: str) -> int: #JUST by regex
|
||||
"""
|
||||
return len(re.findall(reimb_regex, exhibit_text))
|
||||
|
||||
def is_empty(value):
|
||||
def is_empty(value, pd_mask=True):
|
||||
"""
|
||||
Checks if a value is considered empty or invalid.
|
||||
|
||||
Args:
|
||||
value: The value to check.
|
||||
Can be a string, list, or pandas Series.
|
||||
pd_mask (bool): Only relevant for Series inputs.
|
||||
If True, returns a mask for empty values in a pandas Series.
|
||||
Otherwise, the function returns True iff the entire Series is empty.
|
||||
|
||||
Returns:
|
||||
bool: True if the value is empty or invalid, False otherwise.
|
||||
"""
|
||||
empty_values = [None, "", "N/A", "NA", "null", "none", "NaN", np.nan, "nan", "None"]
|
||||
|
||||
if isinstance(value, list): # Handle list inputs
|
||||
return not value or all(is_empty(v) for v in value)
|
||||
|
||||
|
||||
# if it's a pd.Series, return the mask (True for empty values)
|
||||
if isinstance(value, pd.Series):
|
||||
return value.isna() | (value.isin(empty_values)) if pd_mask else value.isna().all()
|
||||
|
||||
if pd.isna(value):
|
||||
return True
|
||||
else:
|
||||
empty_values = [None, "", "N/A", "NA", "null", "none", "NaN", np.nan, "nan", "None"]
|
||||
return value in empty_values
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user