Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
batterseapower committed Sep 25, 2019
1 parent 7df795a commit 5d3a861
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2492,7 +2492,7 @@ def _union(self, other, sort):
# find indexes of things in "other" that are not in "self"
if self.is_unique:
indexer = self.get_indexer(other)
indexer, = (indexer == -1).nonzero()
indexer = (indexer == -1).nonzero()[0]
else:
indexer = algos.unique1d(self.get_indexer_non_unique(other)[1])

Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/indexes/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,24 +600,24 @@ def test_reindex_dtype(self):

def test_reindex_duplicate_source(self):
# See GH23963
c = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
cat = CategoricalIndex(["a", "b", "c", "a"], categories=["a", "b", "c", "d"])
with pytest.raises(ValueError, match="duplicate axis"):
c._can_reindex(["a", "c"])
cat._can_reindex(["a", "c"])

with pytest.raises(ValueError, match="duplicate axis"):
c._can_reindex(
cat._can_reindex(
CategoricalIndex(["a", "c"], categories=["a", "b", "c", "d"])
)

def test_reindex_duplicate_target(self):
# See GH25459
c = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
res, indexer = c.reindex(["a", "c", "c"])
cat = CategoricalIndex(["a", "b", "c"], categories=["a", "b", "c", "d"])
res, indexer = cat.reindex(["a", "c", "c"])
exp = Index(["a", "c", "c"], dtype="object")
tm.assert_index_equal(res, exp, exact=True)
tm.assert_numpy_array_equal(indexer, np.array([0, 2, 2], dtype=np.intp))

res, indexer = c.reindex(
res, indexer = cat.reindex(
CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
)
exp = CategoricalIndex(["a", "c", "c"], categories=["a", "b", "c", "d"])
Expand Down
45 changes: 36 additions & 9 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,27 +273,54 @@ def test_logical_ops_with_index(self, op):
result = op(ser, idx2)
assert_series_equal(result, expected)

def test_reversed_xor_with_index_returns_index(self):
# GH#22092, GH#19792
ser = Series([True, True, False, False])
idx1 = Index([True, False, True, False])
idx2 = Index([1, 0, 1, 0])

expected = Index.symmetric_difference(idx1, ser)
result = idx1 ^ ser
assert_index_equal(result, expected)

expected = Index.symmetric_difference(idx2, ser)
result = idx2 ^ ser
assert_index_equal(result, expected)

@pytest.mark.parametrize(
"op, index_op",
"op",
[
(ops.rand_, Index.intersection),
(ops.ror_, Index.union),
(ops.rxor, Index.symmetric_difference),
pytest.param(
ops.rand_,
marks=pytest.mark.xfail(
reason="GH#22092 Index __and__ returns Index intersection",
raises=AssertionError,
strict=True,
),
),
pytest.param(
ops.ror_,
marks=pytest.mark.xfail(
reason="GH#22092 Index __or__ returns Index union",
raises=AssertionError,
strict=True,
),
),
],
)
def test_reversed_logical_ops_with_index(self, op, index_op):
def test_reversed_logical_op_with_index_returns_series(self, op):
# GH#22092, GH#19792
ser = Series([True, True, False, False])
idx1 = Index([True, False, True, False])
idx2 = Index([1, 0, 1, 0])

expected = index_op(idx1, ser)
expected = pd.Series(op(idx1.values, ser.values))
result = op(ser, idx1)
assert_index_equal(result, expected)
assert_series_equal(result, expected)

expected = index_op(idx2, ser)
expected = pd.Series(op(idx2.values, ser.values))
result = op(ser, idx2)
assert_index_equal(result, expected)
assert_series_equal(result, expected)

@pytest.mark.parametrize(
"op, expected",
Expand Down

0 comments on commit 5d3a861

Please sign in to comment.