Skip to content

Commit 575a852

Browse files
committed
Merge pull request #830 from adamklein/IS811
BUG: close #811, fix index.intersection where indices are incomparable
2 parents ee95ec1 + 75bf87b commit 575a852

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

RELEASE.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pandas 0.7.1
4949
- Fixed bug whereby bool array sometimes had object dtype (#820)
5050
- Fix exception thrown on np.diff (#816)
5151
- Fix to_records where columns are non-strings (#822)
52+
- Fix Index.intersection where indices have incomparable types (#811)
5253

5354
pandas 0.7.0
5455
============

pandas/core/index.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,15 @@ def intersection(self, other):
452452
return this.intersection(other)
453453

454454
if self.is_monotonic and other.is_monotonic:
455-
result = self._inner_indexer(self, other.values)[0]
456-
return self._wrap_union_result(other, result)
457-
else:
458-
indexer = self.get_indexer(other.values)
459-
indexer = indexer.take((indexer != -1).nonzero()[0])
460-
return self.take(indexer)
455+
try:
456+
result = self._inner_indexer(self, other.values)[0]
457+
return self._wrap_union_result(other, result)
458+
except TypeError:
459+
pass
460+
461+
indexer = self.get_indexer(other.values)
462+
indexer = indexer.take((indexer != -1).nonzero()[0])
463+
return self.take(indexer)
461464

462465
def diff(self, other):
463466
"""

pandas/tests/test_frame.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,17 @@ def test_to_records_floats(self):
17451745
df = DataFrame(np.random.rand(10,10))
17461746
df.to_records()
17471747

1748+
def test_join_str_datetime(self):
1749+
str_dates = ['20120209' , '20120222']
1750+
dt_dates = [datetime(2012,2,9), datetime(2012,2,22)]
1751+
1752+
A = DataFrame(str_dates, index=range(2), columns=['aa'])
1753+
C = DataFrame([[1,2],[3,4]], index=str_dates, columns=dt_dates)
1754+
1755+
tst = A.join(C, on = 'aa')
1756+
1757+
self.assert_(len(tst.columns) == 3)
1758+
17481759
def test_from_records_sequencelike(self):
17491760
df = DataFrame({'A' : np.random.randn(6),
17501761
'B' : np.arange(6),

pandas/tests/test_index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,15 @@ def test_intersection(self):
649649
other.values)))
650650
self.assert_(np.array_equal(result, expected))
651651

652+
def test_intersect_str_dates(self):
653+
dt_dates = [datetime(2012,2,9) , datetime(2012,2,22)]
654+
655+
i1 = Index(dt_dates, dtype=object)
656+
i2 = Index(['aa'], dtype=object)
657+
res = i2.intersection(i1)
658+
659+
self.assert_(len(res) == 0)
660+
652661
def test_union_noncomparable(self):
653662
from datetime import datetime, timedelta
654663
# corner case, non-Int64Index

0 commit comments

Comments
 (0)