diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 942aa82906272..3f3ba5224b8af 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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]) diff --git a/pandas/tests/indexes/test_category.py b/pandas/tests/indexes/test_category.py index f3743a579a049..221da492ddc63 100644 --- a/pandas/tests/indexes/test_category.py +++ b/pandas/tests/indexes/test_category.py @@ -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"]) diff --git a/pandas/tests/series/test_operators.py b/pandas/tests/series/test_operators.py index ec2853890b3de..d9b3fd27183ef 100644 --- a/pandas/tests/series/test_operators.py +++ b/pandas/tests/series/test_operators.py @@ -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",