Skip to content

Commit 58e72b6

Browse files
mortadajreback
authored andcommitted
ENH: support __radd__ operation on Index (GH10083)
1 parent 2dc9e0e commit 58e72b6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pandas/core/index.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1181,12 +1181,18 @@ def argsort(self, *args, **kwargs):
11811181
def __add__(self, other):
11821182
if com.is_list_like(other):
11831183
warnings.warn("using '+' to provide set union with Indexes is deprecated, "
1184-
"use '|' or .union()",FutureWarning)
1184+
"use '|' or .union()", FutureWarning)
11851185
if isinstance(other, Index):
11861186
return self.union(other)
11871187
return Index(np.array(self) + other)
1188+
1189+
def __radd__(self, other):
1190+
if com.is_list_like(other):
1191+
warnings.warn("using '+' to provide set union with Indexes is deprecated, "
1192+
"use '|' or .union()", FutureWarning)
1193+
return Index(other + np.array(self))
1194+
11881195
__iadd__ = __add__
1189-
__radd__ = __add__
11901196

11911197
def __sub__(self, other):
11921198
warnings.warn("using '-' to provide set differences with Indexes is deprecated, "

pandas/tests/test_index.py

+7
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,13 @@ def test_add(self):
705705
tm.assert_contains_all(self.strIndex, secondCat)
706706
tm.assert_contains_all(self.dateIndex, firstCat)
707707

708+
# test add and radd
709+
idx = Index(list('abc'))
710+
expected = Index(['a1', 'b1', 'c1'])
711+
self.assert_index_equal(idx + '1', expected)
712+
expected = Index(['1a', '1b', '1c'])
713+
self.assert_index_equal('1' + idx, expected)
714+
708715
def test_append_multiple(self):
709716
index = Index(['a', 'b', 'c', 'd', 'e', 'f'])
710717

0 commit comments

Comments
 (0)