Skip to content

Commit

Permalink
BUG: allow numerical/NA comparisons to flow through in dtype=object a…
Browse files Browse the repository at this point in the history
…rrays, GH #925
  • Loading branch information
wesm committed Mar 16, 2012
1 parent 12d511f commit 814a3c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 16 additions & 10 deletions pandas/src/tseries.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,15 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op):
for i in range(n):
x = left[i]
y = right[i]
if _checknull(x):
result[i] = x
elif _checknull(y):
result[i] = y
else:
try:
result[i] = cpython.PyObject_RichCompareBool(x, y, flag)
except TypeError:
if _checknull(x):
result[i] = x
elif _checknull(y):
result[i] = y
else:
raise

return maybe_convert_bool(result)

Expand Down Expand Up @@ -597,12 +600,15 @@ def vec_binop(ndarray[object] left, ndarray[object] right, object op):
for i in range(n):
x = left[i]
y = right[i]
if _checknull(x):
result[i] = x
elif _checknull(y):
result[i] = y
else:
try:
result[i] = op(x, y)
except TypeError:
if _checknull(x):
result[i] = x
elif _checknull(y):
result[i] = y
else:
raise

return maybe_convert_bool(result)

Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,18 @@ def test_comparison_operators_with_nas(self):
expected = f(s.dropna() < s[9], s.dropna() > s[3]).reindex(s.index)
assert_series_equal(result, expected)

def test_comparison_object_numeric_nas(self):
s = Series(np.random.randn(10), dtype=object)
shifted = s.shift(2)

ops = ['lt', 'le', 'gt', 'ge', 'eq', 'ne']
for op in ops:
f = getattr(operator, op)

result = f(s, shifted)
expected = f(s.astype(float), shifted.astype(float))
assert_series_equal(result, expected)

def test_between(self):
from pandas import DateRange

Expand Down

0 comments on commit 814a3c8

Please sign in to comment.