Skip to content

Commit 330c088

Browse files
committed
ENH: raise TypeError when comparing numeric frame values with non-numeric value, close #943
1 parent a022d45 commit 330c088

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

pandas/core/frame.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2601,8 +2601,13 @@ def _combine_match_columns(self, other, func, fill_value=None):
26012601
def _combine_const(self, other, func):
26022602
if not self:
26032603
return self
2604+
result_values = func(self.values, other)
26042605

2605-
return self._constructor(func(self.values, other), index=self.index,
2606+
if not isinstance(result_values, np.ndarray):
2607+
raise TypeError('Could not compare %s with DataFrame values'
2608+
% repr(other))
2609+
2610+
return self._constructor(result_values, index=self.index,
26062611
columns=self.columns, copy=False)
26072612

26082613
def _compare_frame(self, other, func):

pandas/tests/test_frame.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,12 @@ def _check_unary_op(op):
21102110

21112111
_check_unary_op(operator.neg)
21122112

2113+
def test_logical_typeerror(self):
2114+
self.assertRaises(TypeError, self.frame.__eq__, 'foo')
2115+
self.assertRaises(TypeError, self.frame.__lt__, 'foo')
2116+
self.assertRaises(TypeError, self.frame.__gt__, 'foo')
2117+
self.assertRaises(TypeError, self.frame.__ne__, 'foo')
2118+
21132119
def test_neg(self):
21142120
# what to do?
21152121
assert_frame_equal(-self.frame, -1 * self.frame)
@@ -2518,7 +2524,7 @@ def test_to_excel_from_excel(self):
25182524
reader = ExcelFile(path)
25192525
recons = reader.parse('test1').astype(np.int64)
25202526
assert_frame_equal(frame, recons)
2521-
2527+
25222528
#Test reading/writing np.bool8, roundtrip only works for xlsx
25232529
frame = (DataFrame(np.random.randn(10,2)) >= 0)
25242530
frame.to_excel(path,'test1')
@@ -2552,7 +2558,7 @@ def test_to_excel_from_excel(self):
25522558
recons = reader.parse('test1')
25532559
assert_frame_equal(self.tsframe, recons)
25542560
os.remove(path)
2555-
2561+
25562562
#Test roundtrip np.bool8, does not seem to work for xls
25572563
path = '__tmp__.xlsx'
25582564
frame = (DataFrame(np.random.randn(10,2)) >= 0)

0 commit comments

Comments
 (0)