Repository: pandas-dev/pandas

Issue:
DataFrame.join can not have datetime as column names
Issue:  
    For DataFrameA.join( DataFrameB , on = 'somecol' ), DataFrameB can not have datetime objects as column labels

Example: 

from datetime import datetime
from pandas import DataFrame

str_dates = [ '20120209'         , '20120222'         ]
dt_dates  = [ datetime(2012,2,9) , datetime(2012,2,22)]

A = DataFrame(str_dates     , index = range(2)  , columns = ['aa']   )

B = DataFrame([[1,2],[3,4]] , index = str_dates , columns = str_dates)
C = DataFrame([[1,2],[3,4]] , index = str_dates , columns = dt_dates )

works = A.join( B , on = 'aa' ) # works -- extra column labels are string
fails = A.join( C , on = 'aa' ) # fails -- extra column labels are datetime



Discussion:
The problem is not that the columns cannot be datetime, but rather that strings and datetimes cannot be compared (and thus the resulting columns cannot be ordered). So, if you have on the other hand 

```
A = DataFrame(str_dates , index = range(2) , columns = [datetime(2012,1,1)] )
```

it should all work. eg,

```
In [16]: A = DataFrame(str_dates , index = range(2) , columns = [datetime(2012,1,1)] )

In [17]: A
Out[17]: 
  2012-01-01
0   20120209
1   20120222

In [18]: A.join(C, on=datetime(2012,1,1))
Out[18]: 
  2012-01-01  2012-02-09  2012-02-22
0   20120209           1           2
1   20120222           3           4
```

Not sure what can be done about this one.

This is a bug in `Index.union`. If two indexes are monotonic but their elements are incomparable, self._inner_indexer will fail. Write a unit test with a union of these two indexes:

```
ipdb> self
Index([aa], dtype=object)
ipdb> other
Index([2012-02-09 00:00:00, 2012-02-22 00:00:00], dtype=object)

ipdb> self
Index([aa], dtype=object)
ipdb> other
Index([2012-02-09 00:00:00, 2012-02-22 00:00:00], dtype=object)
```

then add a workaround in case of TypeError from the Cython method


Resolving patch:
diff --git a/pandas/core/index.py b/pandas/core/index.py
--- a/pandas/core/index.py
+++ b/pandas/core/index.py
@@ -452,12 +452,15 @@ def intersection(self, other):
             return this.intersection(other)
 
         if self.is_monotonic and other.is_monotonic:
-            result = self._inner_indexer(self, other.values)[0]
-            return self._wrap_union_result(other, result)
-        else:
-            indexer = self.get_indexer(other.values)
-            indexer = indexer.take((indexer != -1).nonzero()[0])
-            return self.take(indexer)
+            try:
+                result = self._inner_indexer(self, other.values)[0]
+                return self._wrap_union_result(other, result)
+            except TypeError:
+                pass
+
+        indexer = self.get_indexer(other.values)
+        indexer = indexer.take((indexer != -1).nonzero()[0])
+        return self.take(indexer)
 
     def diff(self, other):
         """


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
@@ -1745,6 +1745,17 @@ def test_to_records_floats(self):
         df = DataFrame(np.random.rand(10,10))
         df.to_records()
 
+    def test_join_str_datetime(self):
+        str_dates = ['20120209' , '20120222']
+        dt_dates = [datetime(2012,2,9), datetime(2012,2,22)]
+
+        A = DataFrame(str_dates, index=range(2), columns=['aa'])
+        C = DataFrame([[1,2],[3,4]], index=str_dates, columns=dt_dates)
+
+        tst = A.join(C, on = 'aa')
+
+        self.assert_(len(tst.columns) == 3)
+
     def test_from_records_sequencelike(self):
         df = DataFrame({'A' : np.random.randn(6),
                         'B' : np.arange(6),
diff --git a/pandas/tests/test_index.py b/pandas/tests/test_index.py
--- a/pandas/tests/test_index.py
+++ b/pandas/tests/test_index.py
@@ -649,6 +649,15 @@ def test_intersection(self):
                                                      other.values)))
         self.assert_(np.array_equal(result, expected))
 
+    def test_intersect_str_dates(self):
+        dt_dates = [datetime(2012,2,9) , datetime(2012,2,22)]
+
+        i1 = Index(dt_dates, dtype=object)
+        i2 = Index(['aa'], dtype=object)
+        res = i2.intersection(i1)
+
+        self.assert_(len(res) == 0)
+
     def test_union_noncomparable(self):
         from datetime import datetime, timedelta
         # corner case, non-Int64Index