Skip to content

Commit e797713

Browse files
author
tp
committed
Deprecate .add_prefix and .add_suffix
1 parent cdebcf3 commit e797713

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

Diff for: 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

Diff for: pandas/core/generic.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ class NDFrame(PandasObject, SelectionMixin):
114114
'__array_interface__']
115115
_internal_names_set = set(_internal_names)
116116
_accessors = frozenset([])
117-
_deprecations = frozenset(['as_blocks', 'blocks',
118-
'consolidate', 'convert_objects', 'is_copy'])
117+
_deprecations = frozenset(['as_blocks', 'add_prefix', 'add_suffix',
118+
'blocks', 'consolidate', 'convert_objects',
119+
'is_copy'])
119120
_metadata = []
120121
_is_copy = None
121122

@@ -801,6 +802,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
801802
1 2
802803
4 3
803804
dtype: int64
805+
>>> s.rename('index_{}'.format) # function, changes labels
806+
index_0 1
807+
index_1 2
808+
index_2 3
809+
dtype: object
804810
>>> s.rename({1: 3, 2: 5}) # mapping, changes labels
805811
0 1
806812
3 2
@@ -824,11 +830,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
824830
We *highly* recommend using keyword arguments to clarify your
825831
intent.
826832
827-
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
828-
a c
829-
0 1 4
830-
1 2 5
831-
2 3 6
833+
>>> df.rename(index="index_{}".format, columns={"A": "a", "B": "c"})
834+
a c
835+
index_0 1 4
836+
index_1 2 5
837+
index_2 3 6
832838
833839
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
834840
a B
@@ -2922,6 +2928,8 @@ def _update_inplace(self, result, verify_is_copy=True):
29222928

29232929
def add_prefix(self, prefix):
29242930
"""
2931+
DEPRECATED: Use ``obj.rename(columns=lambda x: 'prefix_'+str(x))`` or similar instead.
2932+
29252933
Concatenate prefix string with panel items names.
29262934
29272935
Parameters
@@ -2931,12 +2939,19 @@ def add_prefix(self, prefix):
29312939
Returns
29322940
-------
29332941
with_prefix : type of caller
2942+
2943+
See Also:
2944+
---------
2945+
rename : Alter axes labels.
29342946
"""
2947+
warnings.warn("'.add_prefix' is deprecated and will be removed in a "
2948+
"future version. Use '.rename' instead", FutureWarning, stacklevel=2)
29352949
new_data = self._data.add_prefix(prefix)
29362950
return self._constructor(new_data).__finalize__(self)
29372951

29382952
def add_suffix(self, suffix):
29392953
"""
2954+
DEPRECATED: Use ``obj.rename(columns=lambda x: 'prefix_'+str(x))`` or similar instead.
29402955
Concatenate suffix string with panel items names.
29412956
29422957
Parameters
@@ -2946,7 +2961,13 @@ def add_suffix(self, suffix):
29462961
Returns
29472962
-------
29482963
with_suffix : type of caller
2964+
2965+
See Also:
2966+
---------
2967+
rename : Alter axes labels.
29492968
"""
2969+
warnings.warn("'add_suffix' is deprecated and will be removed in a "
2970+
"future version.", FutureWarning, stacklevel=2)
29502971
new_data = self._data.add_suffix(suffix)
29512972
return self._constructor(new_data).__finalize__(self)
29522973

Diff for: pandas/tests/frame/test_api.py

+16-12
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)
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)
8283

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)
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)
8688

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)
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)
9093

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