Skip to content

Commit dbc445e

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

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
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

+16-1
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,9 @@ def _update_inplace(self, result, verify_is_copy=True):
25902590

25912591
def add_prefix(self, prefix):
25922592
"""
2593+
DEPRECATED: Use ``obj.rename('prefix_{}'.format)`` or similar instead.
2594+
2595+
25932596
Concatenate prefix string with panel items names.
25942597
25952598
Parameters
@@ -2599,13 +2602,19 @@ def add_prefix(self, prefix):
25992602
Returns
26002603
-------
26012604
with_prefix : type of caller
2605+
2606+
See Also:
2607+
---------
2608+
rename : Alter axes labels.
26022609
"""
2610+
warnings.warn("'add_prefix' is deprecated and will be removed in a "
2611+
"future version.", FutureWarning, stacklevel=2)
26032612
new_data = self._data.add_prefix(prefix)
26042613
return self._constructor(new_data).__finalize__(self)
26052614

26062615
def add_suffix(self, suffix):
26072616
"""
2608-
Concatenate suffix string with panel items names.
2617+
DEPRECATED: Use ``obj.rename('{}_suffix'.format)`` or similar instead.
26092618
26102619
Parameters
26112620
----------
@@ -2614,7 +2623,13 @@ def add_suffix(self, suffix):
26142623
Returns
26152624
-------
26162625
with_suffix : type of caller
2626+
2627+
See Also:
2628+
---------
2629+
rename : Alter axes labels.
26172630
"""
2631+
warnings.warn("'add_suffix' is deprecated and will be removed in a "
2632+
"future version.", FutureWarning, stacklevel=2)
26182633
new_data = self._data.add_suffix(suffix)
26192634
return self._constructor(new_data).__finalize__(self)
26202635

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)