Skip to content

Commit 4bc7b54

Browse files
committed
ENH: Series.append now has ignore_index kw
1 parent d7c028d commit 4bc7b54

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ Other enhancements
249249
- ``pd.read_html()`` has gained support for the ``decimal`` option (:issue:`12907`)
250250
- A function :func:`union_categorical` has been added for combining categoricals, see :ref:`Unioning Categoricals<categorical.union>` (:issue:`13361`)
251251
- ``Series`` has gained the properties ``.is_monotonic``, ``.is_monotonic_increasing``, ``.is_monotonic_decreasing``, similar to ``Index`` (:issue:`13336`)
252+
- ``Series.append`` now supports ``ignore_index`` option (:issue:`13677`)
252253

253254
.. _whatsnew_0190.api:
254255

pandas/core/series.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1511,13 +1511,18 @@ def searchsorted(self, v, side='left', sorter=None):
15111511
# -------------------------------------------------------------------
15121512
# Combination
15131513

1514-
def append(self, to_append, verify_integrity=False):
1514+
def append(self, to_append, ignore_index=False, verify_integrity=False):
15151515
"""
15161516
Concatenate two or more Series.
15171517
15181518
Parameters
15191519
----------
15201520
to_append : Series or list/tuple of Series
1521+
ignore_index : boolean, default False
1522+
If True, do not use the index labels.
1523+
1524+
.. versionadded: 0.19.0
1525+
15211526
verify_integrity : boolean, default False
15221527
If True, raise Exception on creating index with duplicates
15231528
@@ -1548,6 +1553,17 @@ def append(self, to_append, verify_integrity=False):
15481553
5 6
15491554
dtype: int64
15501555
1556+
With `ignore_index` set to True:
1557+
1558+
>>> s1.append(s2, ignore_index=True)
1559+
0 1
1560+
1 2
1561+
2 3
1562+
3 4
1563+
4 5
1564+
5 6
1565+
dtype: int64
1566+
15511567
With `verify_integrity` set to True:
15521568
15531569
>>> s1.append(s2, verify_integrity=True)
@@ -1561,7 +1577,7 @@ def append(self, to_append, verify_integrity=False):
15611577
to_concat = [self] + to_append
15621578
else:
15631579
to_concat = [self, to_append]
1564-
return concat(to_concat, ignore_index=False,
1580+
return concat(to_concat, ignore_index=ignore_index,
15651581
verify_integrity=verify_integrity)
15661582

15671583
def _binop(self, other, func, level=None, fill_value=None):

pandas/tests/series/test_combine_concat.py

+21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ def test_append_many(self):
3939
result = pieces[0].append(pieces[1:])
4040
assert_series_equal(result, self.ts)
4141

42+
def test_append_duplicates(self):
43+
# GH 13677
44+
s1 = pd.Series([1, 2, 3])
45+
s2 = pd.Series([4, 5, 6])
46+
exp = pd.Series([1, 2, 3, 4, 5, 6], index=[0, 1, 2, 0, 1, 2])
47+
tm.assert_series_equal(s1.append(s2), exp)
48+
tm.assert_series_equal(pd.concat([s1, s2]), exp)
49+
50+
# the result must have RangeIndex
51+
exp = pd.Series([1, 2, 3, 4, 5, 6])
52+
tm.assert_series_equal(s1.append(s2, ignore_index=True),
53+
exp, check_index_type=True)
54+
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True),
55+
exp, check_index_type=True)
56+
57+
msg = 'Indexes have overlapping values:'
58+
with tm.assertRaisesRegexp(ValueError, msg):
59+
s1.append(s2, verify_integrity=True)
60+
with tm.assertRaisesRegexp(ValueError, msg):
61+
pd.concat([s1, s2], verify_integrity=True)
62+
4263
def test_combine_first(self):
4364
values = tm.makeIntIndex(20).values.astype(float)
4465
series = Series(values, index=tm.makeIntIndex(20))

0 commit comments

Comments
 (0)