Skip to content

Commit 6725dce

Browse files
authored
Merge pull request #54 from codeflash-ai/handle-pandas-/-np-nan-values
compare pandas NA ambiguous objects correct
2 parents fb27a4c + df220a5 commit 6725dce

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

codeflash/verification/comparator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ def comparator(orig: Any, new: Any, superset_obj=False) -> bool:
155155

156156
if HAS_PANDAS and isinstance(orig, (pandas.CategoricalDtype, pandas.Interval, pandas.Period)):
157157
return orig == new
158+
if HAS_PANDAS and pandas.isna(orig) and pandas.isna(new):
159+
return True
158160

159161
# This should be at the end of all numpy checking
160162
try:

tests/test_comparator.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,36 @@ def test_pandas():
468468
assert comparator(an, ao)
469469
assert not comparator(an, ap)
470470

471+
assert comparator(pd.NA, pd.NA)
472+
assert not comparator(pd.NA, None)
473+
assert not comparator(None, pd.NA)
474+
475+
s1 = pd.Series([1, 2, pd.NA, 4])
476+
s2 = pd.Series([1, 2, pd.NA, 4])
477+
s3 = pd.Series([1, 2, None, 4])
478+
479+
assert comparator(s1, s2)
480+
assert not comparator(s1, s3)
481+
482+
df1 = pd.DataFrame({'a': [1, 2, pd.NA], 'b': [4, pd.NA, 6]})
483+
df2 = pd.DataFrame({'a': [1, 2, pd.NA], 'b': [4, pd.NA, 6]})
484+
df3 = pd.DataFrame({'a': [1, 2, None], 'b': [4, None, 6]})
485+
assert comparator(df1, df2)
486+
assert not comparator(df1, df3)
487+
488+
d1 = {'a': pd.NA, 'b': [1, pd.NA, 3]}
489+
d2 = {'a': pd.NA, 'b': [1, pd.NA, 3]}
490+
d3 = {'a': None, 'b': [1, None, 3]}
491+
assert comparator(d1, d2)
492+
assert not comparator(d1, d3)
493+
494+
s1 = pd.Series([1, 2, pd.NA, 4])
495+
s2 = pd.Series([1, 2, pd.NA, 4])
496+
497+
filtered1 = s1[s1 > 1]
498+
filtered2 = s2[s2 > 1]
499+
assert comparator(filtered1, filtered2)
500+
471501

472502
def test_pyrsistent():
473503
try:

0 commit comments

Comments
 (0)