Skip to content

Commit b125c7b

Browse files
author
tp
committed
Deprecate add_prefix and add_postfix
1 parent cfad581 commit b125c7b

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
lines changed

doc/source/10min.rst

+7-9
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,13 @@ will be completed:
8787
df2.A df2.bool
8888
df2.abs df2.boxplot
8989
df2.add df2.C
90-
df2.add_prefix df2.clip
91-
df2.add_suffix df2.clip_lower
92-
df2.align df2.clip_upper
93-
df2.all df2.columns
94-
df2.any df2.combine
95-
df2.append df2.combine_first
96-
df2.apply df2.compound
97-
df2.applymap df2.consolidate
98-
df2.D
90+
df2.align df2.clip
91+
df2.all df2.clip_lower
92+
df2.any df2.clip_upper
93+
df2.append df2.columns
94+
df2.apply df2.combine
95+
df2.applymap df2.combine_first
96+
df2.D df2.compound
9997

10098
As you can see, the columns ``A``, ``B``, ``C``, and ``D`` are automatically
10199
tab completed. ``E`` is there as well; the rest of the attributes have been

doc/source/api.rst

-4
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,6 @@ Reindexing / Selection / Label manipulation
10101010
.. autosummary::
10111011
:toctree: generated/
10121012

1013-
DataFrame.add_prefix
1014-
DataFrame.add_suffix
10151013
DataFrame.align
10161014
DataFrame.at_time
10171015
DataFrame.between_time
@@ -1317,8 +1315,6 @@ Reindexing / Selection / Label manipulation
13171315
.. autosummary::
13181316
:toctree: generated/
13191317

1320-
Panel.add_prefix
1321-
Panel.add_suffix
13221318
Panel.drop
13231319
Panel.equals
13241320
Panel.filter

doc/source/whatsnew/v0.22.0.txt

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

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

6162
.. _whatsnew_0220.prior_deprecations:

pandas/core/generic.py

+26-6
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,9 @@ 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+
2600+
25932601
Concatenate prefix string with panel items names.
25942602
25952603
Parameters
@@ -2599,13 +2607,19 @@ def add_prefix(self, prefix):
25992607
Returns
26002608
-------
26012609
with_prefix : type of caller
2610+
2611+
See Also:
2612+
---------
2613+
rename : Alter axes labels.
26022614
"""
2615+
warnings.warn("'add_prefix' is deprecated and will be removed in a "
2616+
"future version.", FutureWarning, stacklevel=2)
26032617
new_data = self._data.add_prefix(prefix)
26042618
return self._constructor(new_data).__finalize__(self)
26052619

26062620
def add_suffix(self, suffix):
26072621
"""
2608-
Concatenate suffix string with panel items names.
2622+
DEPRECATED: Use ``obj.rename('{}_suffix'.format)`` or similar instead.
26092623
26102624
Parameters
26112625
----------
@@ -2614,7 +2628,13 @@ def add_suffix(self, suffix):
26142628
Returns
26152629
-------
26162630
with_suffix : type of caller
2631+
2632+
See Also:
2633+
---------
2634+
rename : Alter axes labels.
26172635
"""
2636+
warnings.warn("'add_suffix' is deprecated and will be removed in a "
2637+
"future version.", FutureWarning, stacklevel=2)
26182638
new_data = self._data.add_suffix(suffix)
26192639
return self._constructor(new_data).__finalize__(self)
26202640

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 pytest.warns(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 pytest.warns(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 pytest.warns(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 pytest.warns(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)