Skip to content

Commit 814a3c8

Browse files
committed
BUG: allow numerical/NA comparisons to flow through in dtype=object arrays, GH pandas-dev#925
1 parent 12d511f commit 814a3c8

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

pandas/src/tseries.pyx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,15 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
558558
for i in range(n):
559559
x = left[i]
560560
y = right[i]
561-
if _checknull(x):
562-
result[i] = x
563-
elif _checknull(y):
564-
result[i] = y
565-
else:
561+
try:
566562
result[i] = cpython.PyObject_RichCompareBool(x, y, flag)
563+
except TypeError:
564+
if _checknull(x):
565+
result[i] = x
566+
elif _checknull(y):
567+
result[i] = y
568+
else:
569+
raise
567570

568571
return maybe_convert_bool(result)
569572

@@ -597,12 +600,15 @@ def vec_binop(ndarray[object] left, ndarray[object] right, object op):
597600
for i in range(n):
598601
x = left[i]
599602
y = right[i]
600-
if _checknull(x):
601-
result[i] = x
602-
elif _checknull(y):
603-
result[i] = y
604-
else:
603+
try:
605604
result[i] = op(x, y)
605+
except TypeError:
606+
if _checknull(x):
607+
result[i] = x
608+
elif _checknull(y):
609+
result[i] = y
610+
else:
611+
raise
606612

607613
return maybe_convert_bool(result)
608614

pandas/tests/test_series.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,18 @@ def test_comparison_operators_with_nas(self):
11601160
expected = f(s.dropna() < s[9], s.dropna() > s[3]).reindex(s.index)
11611161
assert_series_equal(result, expected)
11621162

1163+
def test_comparison_object_numeric_nas(self):
1164+
s = Series(np.random.randn(10), dtype=object)
1165+
shifted = s.shift(2)
1166+
1167+
ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne']
1168+
for op in ops:
1169+
f = getattr(operator, op)
1170+
1171+
result = f(s, shifted)
1172+
expected = f(s.astype(float), shifted.astype(float))
1173+
assert_series_equal(result, expected)
1174+
11631175
def test_between(self):
11641176
from pandas import DateRange
11651177

0 commit comments

Comments
 (0)