Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions cdisc_rules_engine/check_operators/dataframe_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,19 @@ def __init__(self, data):
def _assert_valid_value_and_cast(self, value):
return value

def _regex_str_conversion(self, x):
"""Convert value to string for regex operations.
Only converts non-null values. Returns NaN/None as-is.
"""
if pd.notna(x):
if isinstance(x, int):
return str(x).strip()
elif isinstance(x, float):
return f"{x:.0f}" if x.is_integer() else str(x).strip()
return x

def _custom_str_conversion(self, x):
"""used to normalize numeric representations i.e. treat 200.00 as 200 for comparisons"""
if pd.notna(x):
if isinstance(x, str):
try:
Expand Down Expand Up @@ -750,7 +762,7 @@ def prefix_matches_regex(self, other_value):
comparator = other_value.get("comparator")
prefix = other_value.get("prefix")
converted_strings = self.value[target].map(
lambda x: self._custom_str_conversion(x)
lambda x: self._regex_str_conversion(x)
)
results = converted_strings.notna() & converted_strings.astype(str).map(
lambda x: re.search(comparator, x[:prefix]) is not None
Expand All @@ -764,7 +776,7 @@ def not_prefix_matches_regex(self, other_value):
comparator = other_value.get("comparator")
prefix = other_value.get("prefix")
converted_strings = self.value[target].map(
lambda x: self._custom_str_conversion(x)
lambda x: self._regex_str_conversion(x)
)
results = converted_strings.notna() & ~converted_strings.astype(str).map(
lambda x: re.search(comparator, x[:prefix]) is not None
Expand All @@ -778,7 +790,7 @@ def suffix_matches_regex(self, other_value):
comparator = other_value.get("comparator")
suffix = other_value.get("suffix")
converted_strings = self.value[target].map(
lambda x: self._custom_str_conversion(x)
lambda x: self._regex_str_conversion(x)
)
results = converted_strings.notna() & converted_strings.astype(str).map(
lambda x: re.search(comparator, x[-suffix:]) is not None
Expand All @@ -792,7 +804,7 @@ def not_suffix_matches_regex(self, other_value):
comparator = other_value.get("comparator")
suffix = other_value.get("suffix")
converted_strings = self.value[target].map(
lambda x: self._custom_str_conversion(x)
lambda x: self._regex_str_conversion(x)
)
results = converted_strings.notna() & ~converted_strings.astype(str).map(
lambda x: re.search(comparator, x[-suffix:]) is not None
Expand All @@ -805,7 +817,7 @@ def matches_regex(self, other_value):
target = self.replace_prefix(other_value.get("target"))
comparator = other_value.get("comparator")
converted_strings = self.value[target].map(
lambda x: self._custom_str_conversion(x)
lambda x: self._regex_str_conversion(x)
)
results = converted_strings.notna() & converted_strings.astype(str).str.match(
comparator
Expand All @@ -818,7 +830,7 @@ def not_matches_regex(self, other_value):
target = self.replace_prefix(other_value.get("target"))
comparator = other_value.get("comparator")
converted_strings = self.value[target].map(
lambda x: self._custom_str_conversion(x)
lambda x: self._regex_str_conversion(x)
)
results = converted_strings.notna() & ~converted_strings.astype(str).str.match(
comparator
Expand Down
Loading