Skip to content

Commit 20487bf

Browse files
jschendeljowens
authored andcommitted
ENH: Support strings containing '%' in add_prefix/add_suffix (pandas-dev#17151) (pandas-dev#17162)
1 parent d2e21c3 commit 20487bf

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ Other Enhancements
8181
- :func:`date_range` now accepts 'YS' in addition to 'AS' as an alias for start of year (:issue:`9313`)
8282
- :func:`date_range` now accepts 'Y' in addition to 'A' as an alias for end of year (:issue:`9313`)
8383
- :func:`read_html` handles colspan and rowspan arguments and attempts to infer a header if the header is not explicitly specified (:issue:`17054`)
84-
- Integration with `Apache Parquet <https://parquet.apache.org/>`__, including a new top-level :func:`pd.read_parquet` and :func:`DataFrame.to_parquet` method, see :ref:`here <io.parquet>`.
84+
- Integration with `Apache Parquet <https://parquet.apache.org/>`__, including a new top-level :func:`read_parquet` and :func:`DataFrame.to_parquet` method, see :ref:`here <io.parquet>`.
85+
- :func:`DataFrame.add_prefix` and :func:`DataFrame.add_suffix` now accept strings containing the '%' character. (:issue:`17151`)
8586

8687
.. _whatsnew_0210.api_breaking:
8788

pandas/core/internals.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import operator
66
from datetime import datetime, timedelta, date
77
from collections import defaultdict
8+
from functools import partial
89

910
import numpy as np
1011

@@ -2959,11 +2960,11 @@ def rename_axis(self, mapper, axis, copy=True, level=None):
29592960
return obj
29602961

29612962
def add_prefix(self, prefix):
2962-
f = (str(prefix) + '%s').__mod__
2963+
f = partial('{prefix}{}'.format, prefix=prefix)
29632964
return self.rename_axis(f, axis=0)
29642965

29652966
def add_suffix(self, suffix):
2966-
f = ('%s' + str(suffix)).__mod__
2967+
f = partial('{}{suffix}'.format, suffix=suffix)
29672968
return self.rename_axis(f, axis=0)
29682969

29692970
@property

pandas/tests/frame/test_api.py

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ def test_add_prefix_suffix(self):
6868
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
6969
tm.assert_index_equal(with_suffix.columns, expected)
7070

71+
with_pct_prefix = self.frame.add_prefix('%')
72+
expected = pd.Index(['%{}'.format(c) for c in self.frame.columns])
73+
tm.assert_index_equal(with_pct_prefix.columns, expected)
74+
75+
with_pct_suffix = self.frame.add_suffix('%')
76+
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns])
77+
tm.assert_index_equal(with_pct_suffix.columns, expected)
78+
7179

7280
class TestDataFrameMisc(SharedWithSparse, TestData):
7381

0 commit comments

Comments
 (0)