From 75bf87b8b665fcee0c55e14ebb6a3957d20f9dba Mon Sep 17 00:00:00 2001 From: Adam Klein Date: Fri, 24 Feb 2012 17:22:49 -0500 Subject: [PATCH] BUG: close #811, fix index.intersection where indices are incomparable --- RELEASE.rst | 1 + pandas/core/index.py | 15 +++++++++------ pandas/tests/test_frame.py | 11 +++++++++++ pandas/tests/test_index.py | 9 +++++++++ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/RELEASE.rst b/RELEASE.rst index 946bd47ad05b3..5033ff07a00c8 100644 --- a/RELEASE.rst +++ b/RELEASE.rst @@ -49,6 +49,7 @@ pandas 0.7.1 - Fixed bug whereby bool array sometimes had object dtype (#820) - Fix exception thrown on np.diff (#816) - Fix to_records where columns are non-strings (#822) + - Fix Index.intersection where indices have incomparable types (#811) pandas 0.7.0 ============ diff --git a/pandas/core/index.py b/pandas/core/index.py index 8b8b3f598f978..1d8e3f0729afe 100644 --- 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): """ diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 65fa2a48591d2..21afaedac53d5 100644 --- 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 index 998c7ff037e68..474e37b5e9e11 100644 --- 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