Skip to content

Commit 79ab0aa

Browse files
author
tp
committed
Deprecate .add_prefix and .add_suffix
1 parent b00e62c commit 79ab0aa

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

doc/source/whatsnew/v0.22.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ Deprecations
5656
~~~~~~~~~~~~
5757

5858
- ``Series.from_array`` and ``SparseSeries.from_array`` are deprecated. Use the normal constructor ``Series(..)`` and ``SparseSeries(..)`` instead (:issue:`18213`).
59-
-
59+
- ``DataFrame.add_prefix``, ``DataFrame.add_suffix``, ``Series.add_prefix`` and ``Series.add_suffix``
60+
have been deprecated. Use the form ``obj.rename('prefix_{}_suffix'.format)`` or similar instead (:issue:`18347`).
6061
-
6162

6263
.. _whatsnew_0220.prior_deprecations:

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ def _constructor(self):
304304

305305
_constructor_sliced = Series
306306
_deprecations = NDFrame._deprecations | frozenset(
307-
['sortlevel', 'get_value', 'set_value', 'from_csv'])
307+
['sortlevel', 'get_value', 'set_value', 'from_csv', 'add_prefix',
308+
'add_suffix'])
308309

309310
@property
310311
def _constructor_expanddim(self):

pandas/core/generic.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
785785
1 2
786786
4 3
787787
dtype: int64
788+
>>> s.rename('index_{}'.format) # function, changes labels
789+
index_0 1
790+
index_1 2
791+
index_2 3
792+
dtype: object
788793
>>> s.rename({1: 3, 2: 5}) # mapping, changes labels
789794
0 1
790795
3 2
@@ -808,11 +813,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
808813
We *highly* recommend using keyword arguments to clarify your
809814
intent.
810815
811-
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
812-
a c
813-
0 1 4
814-
1 2 5
815-
2 3 6
816+
>>> df.rename(index="index_{}".format, columns={"A": "a", "B": "c"})
817+
a c
818+
index_0 1 4
819+
index_1 2 5
820+
index_2 3 6
816821
817822
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
818823
a B
@@ -2590,6 +2595,8 @@ def _update_inplace(self, result, verify_is_copy=True):
25902595

25912596
def add_prefix(self, prefix):
25922597
"""
2598+
DEPRECATED: Use ``obj.rename('prefix_{}'.format)`` or similar instead.
2599+
25932600
Concatenate prefix string with panel items names.
25942601
25952602
Parameters
@@ -2599,12 +2606,20 @@ def add_prefix(self, prefix):
25992606
Returns
26002607
-------
26012608
with_prefix : type of caller
2609+
2610+
See Also:
2611+
---------
2612+
rename : Alter axes labels.
26022613
"""
2614+
warnings.warn("'add_prefix' is deprecated and will be removed in a "
2615+
"future version.", FutureWarning, stacklevel=2)
26032616
new_data = self._data.add_prefix(prefix)
26042617
return self._constructor(new_data).__finalize__(self)
26052618

26062619
def add_suffix(self, suffix):
26072620
"""
2621+
DEPRECATED: Use ``obj.rename('{}_suffix'.format)`` or similar instead.
2622+
26082623
Concatenate suffix string with panel items names.
26092624
26102625
Parameters
@@ -2614,7 +2629,13 @@ def add_suffix(self, suffix):
26142629
Returns
26152630
-------
26162631
with_suffix : type of caller
2632+
2633+
See Also:
2634+
---------
2635+
rename : Alter axes labels.
26172636
"""
2637+
warnings.warn("'add_suffix' is deprecated and will be removed in a "
2638+
"future version.", FutureWarning, stacklevel=2)
26182639
new_data = self._data.add_suffix(suffix)
26192640
return self._constructor(new_data).__finalize__(self)
26202641

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
149149
_metadata = ['name']
150150
_accessors = frozenset(['dt', 'cat', 'str'])
151151
_deprecations = generic.NDFrame._deprecations | frozenset(
152-
['sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv'])
152+
['sortlevel', 'reshape', 'get_value', 'set_value', 'from_csv',
153+
'add_prefix', 'add_suffix'])
153154
_allow_index_ops = True
154155

155156
def __init__(self, data=None, index=None, dtype=None, name=None,

pandas/tests/frame/test_api.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,25 @@ def test_get_value(self):
7676
tm.assert_almost_equal(result, expected)
7777

7878
def test_add_prefix_suffix(self):
79-
with_prefix = self.frame.add_prefix('foo#')
80-
expected = pd.Index(['foo#%s' % c for c in self.frame.columns])
81-
tm.assert_index_equal(with_prefix.columns, expected)
82-
83-
with_suffix = self.frame.add_suffix('#foo')
84-
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
85-
tm.assert_index_equal(with_suffix.columns, expected)
86-
87-
with_pct_prefix = self.frame.add_prefix('%')
88-
expected = pd.Index(['%{}'.format(c) for c in self.frame.columns])
89-
tm.assert_index_equal(with_pct_prefix.columns, expected)
90-
91-
with_pct_suffix = self.frame.add_suffix('%')
92-
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns])
93-
tm.assert_index_equal(with_pct_suffix.columns, expected)
79+
with tm.assert_produces_warning(FutureWarning):
80+
with_prefix = self.frame.add_prefix('foo#')
81+
expected = pd.Index(['foo#%s' % c for c in self.frame.columns])
82+
tm.assert_index_equal(with_prefix.columns, expected)
83+
84+
with tm.assert_produces_warning(FutureWarning):
85+
with_suffix = self.frame.add_suffix('#foo')
86+
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
87+
tm.assert_index_equal(with_suffix.columns, expected)
88+
89+
with tm.assert_produces_warning(FutureWarning):
90+
with_pct_prefix = self.frame.add_prefix('%')
91+
expected = pd.Index(['%{}'.format(c) for c in self.frame.columns])
92+
tm.assert_index_equal(with_pct_prefix.columns, expected)
93+
94+
with tm.assert_produces_warning(FutureWarning):
95+
with_pct_suffix = self.frame.add_suffix('%')
96+
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns])
97+
tm.assert_index_equal(with_pct_suffix.columns, expected)
9498

9599
def test_get_axis(self):
96100
f = self.frame

0 commit comments

Comments
 (0)