Skip to content

Commit 7bd0e59

Browse files
committed
BUG: provide deprecation warnings when using setlike operations on Indexes and lists (for +/-) (GH10038)
1 parent 66f1ccf commit 7bd0e59

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

pandas/core/index.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1179,18 +1179,20 @@ def argsort(self, *args, **kwargs):
11791179
return result.argsort(*args, **kwargs)
11801180

11811181
def __add__(self, other):
1182-
if isinstance(other, Index):
1182+
if com.is_list_like(other):
11831183
warnings.warn("using '+' to provide set union with Indexes is deprecated, "
11841184
"use '|' or .union()",FutureWarning)
1185+
if isinstance(other, Index):
11851186
return self.union(other)
11861187
return Index(np.array(self) + other)
11871188
__iadd__ = __add__
1189+
__radd__ = __add__
11881190

11891191
def __sub__(self, other):
1190-
if isinstance(other, Index):
1191-
warnings.warn("using '-' to provide set differences with Indexes is deprecated, "
1192-
"use .difference()",FutureWarning)
1192+
warnings.warn("using '-' to provide set differences with Indexes is deprecated, "
1193+
"use .difference()",FutureWarning)
11931194
return self.difference(other)
1195+
__rsub__ = __sub__
11941196

11951197
def __and__(self, other):
11961198
return self.intersection(other)
@@ -2481,8 +2483,8 @@ def invalid_op(self, other=None):
24812483
invalid_op.__name__ = name
24822484
return invalid_op
24832485

2484-
cls.__add__ = cls.__add__ = __iadd__ = _make_invalid_op('__add__')
2485-
cls.__sub__ = cls.__sub__ = __isub__ = _make_invalid_op('__sub__')
2486+
cls.__add__ = cls.__radd__ = __iadd__ = _make_invalid_op('__add__')
2487+
cls.__sub__ = cls.__rsub__ = __isub__ = _make_invalid_op('__sub__')
24862488

24872489
@classmethod
24882490
def _add_numeric_methods_disabled(cls):

pandas/core/reshape.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def _convert_level_number(level_num, columns):
612612
new_data[key] = value_slice.ravel()
613613

614614
if len(drop_cols) > 0:
615-
new_columns = new_columns - drop_cols
615+
new_columns = new_columns.difference(drop_cols)
616616

617617
N = len(this)
618618

@@ -1045,7 +1045,7 @@ def check_len(item, name):
10451045
with_dummies = [result]
10461046
for (col, pre, sep) in zip(columns_to_encode, prefix, prefix_sep):
10471047

1048-
dummy = _get_dummies_1d(data[col], prefix=pre, prefix_sep=sep,
1048+
dummy = _get_dummies_1d(data[col], prefix=pre, prefix_sep=sep,
10491049
dummy_na=dummy_na, sparse=sparse)
10501050
with_dummies.append(dummy)
10511051
result = concat(with_dummies, axis=1)

pandas/tests/test_index.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,10 @@ def test_add(self):
686686
# - API change GH 8226
687687
with tm.assert_produces_warning():
688688
self.strIndex + self.strIndex
689+
with tm.assert_produces_warning():
690+
self.strIndex + self.strIndex.tolist()
691+
with tm.assert_produces_warning():
692+
self.strIndex.tolist() + self.strIndex
689693

690694
firstCat = self.strIndex.union(self.dateIndex)
691695
secondCat = self.strIndex.union(self.strIndex)
@@ -772,6 +776,7 @@ def test_difference(self):
772776
assertRaisesRegexp(TypeError, "iterable", first.difference, 0.5)
773777

774778
def test_symmetric_diff(self):
779+
775780
# smoke
776781
idx1 = Index([1, 2, 3, 4], name='idx1')
777782
idx2 = Index([2, 3, 4, 5])
@@ -819,7 +824,7 @@ def test_symmetric_diff(self):
819824

820825
# other isn't iterable
821826
with tm.assertRaises(TypeError):
822-
Index(idx1,dtype='object') - 1
827+
Index(idx1,dtype='object').difference(1)
823828

824829
def test_is_numeric(self):
825830
self.assertFalse(self.dateIndex.is_numeric())

0 commit comments

Comments
 (0)