Skip to content

Fix bug in outer_indexer where the special case of an empty right array resulted in bogus return data. #10619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Bug Fixes
- Bug in ``MultiIndex.get_level_values`` including ``Categorical`` raises ``AttributeError`` (:issue:`10460`)
- Bug in ``pd.get_dummies`` with `sparse=True` not returning ``SparseDataFrame`` (:issue:`10531`)
- Bug in ``Index`` subtypes (such as ``PeriodIndex``) not returning their own type for ``.drop`` and ``.insert`` methods (:issue:`10620`)
- Bug in ``algos.outer_join_indexer`` when ``right`` array is empty (:issue:`10618`)



Expand Down
2 changes: 1 addition & 1 deletion pandas/src/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2131,7 +2131,7 @@ def outer_join_indexer_%(name)s(ndarray[%(c_type)s] left,
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nright):
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
Expand Down
10 changes: 5 additions & 5 deletions pandas/src/generated.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10345,7 +10345,7 @@ def outer_join_indexer_float64(ndarray[float64_t] left,
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nright):
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
Expand Down Expand Up @@ -10474,7 +10474,7 @@ def outer_join_indexer_float32(ndarray[float32_t] left,
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nright):
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
Expand Down Expand Up @@ -10603,7 +10603,7 @@ def outer_join_indexer_object(ndarray[object] left,
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nright):
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
Expand Down Expand Up @@ -10732,7 +10732,7 @@ def outer_join_indexer_int32(ndarray[int32_t] left,
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nright):
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
Expand Down Expand Up @@ -10861,7 +10861,7 @@ def outer_join_indexer_int64(ndarray[int64_t] left,
rindexer[j] = j
result[j] = right[j]
elif nright == 0:
for i in range(nright):
for i in range(nleft):
lindexer[i] = i
rindexer[i] = -1
result[i] = left[i]
Expand Down
32 changes: 32 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ def _test_vector_resize(htable, uniques, dtype, nvals):
_test_vector_resize(tbl(), vect(), dtype, 0)
_test_vector_resize(tbl(), vect(), dtype, 10)

class TestIndexer(tm.TestCase):
_multiprocess_can_split_ = True

def test_outer_join_indexer(self):
typemap = [('int32', algos.algos.outer_join_indexer_int32),
('int64', algos.algos.outer_join_indexer_int64),
('float32', algos.algos.outer_join_indexer_float32),
('float64', algos.algos.outer_join_indexer_float64),
('object', algos.algos.outer_join_indexer_object)]

for dtype, indexer in typemap:
left = np.arange(3, dtype = dtype)
right = np.arange(2,5, dtype = dtype)
empty = np.array([], dtype = dtype)

result, lindexer, rindexer = indexer(left, right)
tm.assertIsInstance(result, np.ndarray)
tm.assertIsInstance(lindexer, np.ndarray)
tm.assertIsInstance(rindexer, np.ndarray)
tm.assert_numpy_array_equal(result, np.arange(5, dtype = dtype))
tm.assert_numpy_array_equal(lindexer, np.array([0, 1, 2, -1, -1]))
tm.assert_numpy_array_equal(rindexer, np.array([-1, -1, 0, 1, 2]))

result, lindexer, rindexer = indexer(empty, right)
tm.assert_numpy_array_equal(result, right)
tm.assert_numpy_array_equal(lindexer, np.array([-1, -1, -1]))
tm.assert_numpy_array_equal(rindexer, np.array([0, 1, 2]))

result, lindexer, rindexer = indexer(left, empty)
tm.assert_numpy_array_equal(result, left)
tm.assert_numpy_array_equal(lindexer, np.array([0, 1, 2]))
tm.assert_numpy_array_equal(rindexer, np.array([-1, -1, -1]))

class TestUnique(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down