Skip to content

Commit 3997642

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

File tree

4 files changed

+56
-32
lines changed

4 files changed

+56
-32
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.B 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/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/generic.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ class NDFrame(PandasObject, SelectionMixin):
113113
'__array_interface__']
114114
_internal_names_set = set(_internal_names)
115115
_accessors = frozenset([])
116-
_deprecations = frozenset(['as_blocks', 'blocks',
117-
'consolidate', 'convert_objects'])
116+
_deprecations = frozenset(['as_blocks', 'blocks', 'consolidate',
117+
'convert_objects', 'add_prefix', 'add_suffix'])
118118
_metadata = []
119119
is_copy = None
120120

@@ -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/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)