Repository: pandas-dev/pandas

Issue:
eq and ne not consistent with other comparison operators
works

```
In [1]: df = DataFrame(randint(10, size=(10, 1)), columns=['a'])

In [2]: df['dates'] = date_range('20010101', periods=len(df))

In [3]: df.a == df.dates
Out[3]:
0    False
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9    False
dtype: bool
```

doesn't work:

```
In [4]: df.a <= df.dates
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-74e89260dea7> in <module>()
----> 1 df.a <= df.dates

/home/phillip/Documents/code/py/pandas/pandas/core/series.pyc in wrapper(self, other)
    336             if len(self) != len(other):
    337                 raise ValueError('Series lengths must match to compare')
--> 338             return self._constructor(na_op(self.values, other.values),
    339                                      index=self.index, name=name)
    340         elif isinstance(other, DataFrame):  # pragma: no cover

/home/phillip/Documents/code/py/pandas/pandas/core/series.pyc in na_op(x, y)
    325                 result = lib.scalar_compare(x, y, op)
    326         else:
--> 327             result = op(x, y)
    328
    329         return result

TypeError: invalid type promotion
```



Discussion:

Resolving patch:
diff --git a/pandas/core/series.py b/pandas/core/series.py
--- a/pandas/core/series.py
+++ b/pandas/core/series.py
@@ -324,7 +324,13 @@ def na_op(x, y):
             else:
                 result = lib.scalar_compare(x, y, op)
         else:
-            result = op(x, y)
+
+            try:
+                result = getattr(x,name)(y)
+                if result is NotImplemented:
+                    raise TypeError("invalid type comparison")
+            except (AttributeError):
+                result = op(x, y)
 
         return result
 


Test patch:
diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py
--- a/pandas/tests/test_frame.py
+++ b/pandas/tests/test_frame.py
@@ -4296,6 +4296,31 @@ def test_operators_none_as_na(self):
             result = op(df.fillna(7), df)
             assert_frame_equal(result, expected)
 
+    def test_comparison_invalid(self):
+
+        def check(df,df2):
+
+            for (x, y) in [(df,df2),(df2,df)]:
+                self.assertRaises(TypeError, lambda : x == y)
+                self.assertRaises(TypeError, lambda : x != y)
+                self.assertRaises(TypeError, lambda : x >= y)
+                self.assertRaises(TypeError, lambda : x > y)
+                self.assertRaises(TypeError, lambda : x < y)
+                self.assertRaises(TypeError, lambda : x <= y)
+
+        # GH4968
+        # invalid date/int comparisons
+        df = DataFrame(np.random.randint(10, size=(10, 1)), columns=['a'])
+        df['dates'] = date_range('20010101', periods=len(df))
+
+        df2 = df.copy()
+        df2['dates'] = df['a']
+        check(df,df2)
+
+        df = DataFrame(np.random.randint(10, size=(10, 2)), columns=['a', 'b'])
+        df2 = DataFrame({'a': date_range('20010101', periods=len(df)), 'b': date_range('20100101', periods=len(df))})
+        check(df,df2)
+
     def test_modulo(self):
 
         # GH3590, modulo as ints
diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py
--- a/pandas/tests/test_series.py
+++ b/pandas/tests/test_series.py
@@ -2663,6 +2663,21 @@ def test_comparison_object_numeric_nas(self):
             expected = f(s.astype(float), shifted.astype(float))
             assert_series_equal(result, expected)
 
+    def test_comparison_invalid(self):
+
+        # GH4968
+        # invalid date/int comparisons
+        s = Series(range(5))
+        s2 = Series(date_range('20010101', periods=5))
+
+        for (x, y) in [(s,s2),(s2,s)]:
+            self.assertRaises(TypeError, lambda : x == y)
+            self.assertRaises(TypeError, lambda : x != y)
+            self.assertRaises(TypeError, lambda : x >= y)
+            self.assertRaises(TypeError, lambda : x > y)
+            self.assertRaises(TypeError, lambda : x < y)
+            self.assertRaises(TypeError, lambda : x <= y)
+
     def test_more_na_comparisons(self):
         left = Series(['a', np.nan, 'c'])
         right = Series(['a', np.nan, 'd'])