Skip to content

DEPR: deprecate .add_prefix and .add_suffix #18347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions doc/source/10min.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,13 @@ will be completed:
df2.A df2.bool
df2.abs df2.boxplot
df2.add df2.C
df2.add_prefix df2.clip
df2.add_suffix df2.clip_lower
df2.align df2.clip_upper
df2.all df2.columns
df2.any df2.combine
df2.append df2.combine_first
df2.apply df2.compound
df2.applymap df2.consolidate
df2.D
df2.align df2.clip
df2.all df2.clip_lower
df2.any df2.clip_upper
df2.append df2.columns
df2.apply df2.combine
df2.applymap df2.combine_first
df2.B df2.compound

As you can see, the columns ``A``, ``B``, ``C``, and ``D`` are automatically
tab completed. ``E`` is there as well; the rest of the attributes have been
Expand Down
39 changes: 31 additions & 8 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ class NDFrame(PandasObject, SelectionMixin):
'__array_interface__']
_internal_names_set = set(_internal_names)
_accessors = frozenset([])
_deprecations = frozenset(['as_blocks', 'blocks',
'consolidate', 'convert_objects', 'is_copy'])
_deprecations = frozenset(['as_blocks', 'add_prefix', 'add_suffix',
'blocks', 'consolidate', 'convert_objects',
'is_copy'])
_metadata = []
_is_copy = None

Expand Down Expand Up @@ -801,6 +802,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
1 2
4 3
dtype: int64
>>> s.rename('index_{}'.format) # function, changes labels
index_0 1
index_1 2
index_2 3
dtype: object
>>> s.rename({1: 3, 2: 5}) # mapping, changes labels
0 1
3 2
Expand All @@ -824,11 +830,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
We *highly* recommend using keyword arguments to clarify your
intent.

>>> df.rename(index=str, columns={"A": "a", "B": "c"})
a c
0 1 4
1 2 5
2 3 6
>>> df.rename(index="index_{}".format, columns={"A": "a", "B": "c"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any use of .add_prefix/suffix in the main docs? if so can you update

Copy link
Contributor Author

@topper-123 topper-123 Nov 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not used in the docs. It's shown in 10min_to_pandas as a result of tab completion, but that should stay until removal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no please change the docs now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I miswrote: it's not in the docs, expect as a result of that tab comletion example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh these need to be added to
_deprecations in series and dataframe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, added.

a c
index_0 1 4
index_1 2 5
index_2 3 6

>>> df.rename(index=str, columns={"A": "a", "C": "c"})
a B
Expand Down Expand Up @@ -2922,6 +2928,9 @@ def _update_inplace(self, result, verify_is_copy=True):

def add_prefix(self, prefix):
"""
DEPRECATED: Use ``obj.rename(columns=lambda x: 'prefix_'+str(x))`` or
similar instead.

Concatenate prefix string with panel items names.

Parameters
Expand All @@ -2931,13 +2940,21 @@ def add_prefix(self, prefix):
Returns
-------
with_prefix : type of caller

See Also:
---------
rename : Alter axes labels.
"""
warnings.warn("'.add_prefix' is deprecated and will be removed in a "
"future version. Use '.rename' instead",
FutureWarning, stacklevel=2)
new_data = self._data.add_prefix(prefix)
return self._constructor(new_data).__finalize__(self)

def add_suffix(self, suffix):
"""
Concatenate suffix string with panel items names.
DEPRECATED: Use ``obj.rename(columns=lambda x: 'prefix_'+str(x))`` or
similar instead. Concatenate suffix string with panel items names.

Parameters
----------
Expand All @@ -2946,7 +2963,13 @@ def add_suffix(self, suffix):
Returns
-------
with_suffix : type of caller

See Also:
---------
rename : Alter axes labels.
"""
warnings.warn("'add_suffix' is deprecated and will be removed in a "
"future version.", FutureWarning, stacklevel=2)
new_data = self._data.add_suffix(suffix)
return self._constructor(new_data).__finalize__(self)

Expand Down
28 changes: 16 additions & 12 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@ def test_get_value(self):
tm.assert_almost_equal(result, expected)

def test_add_prefix_suffix(self):
with_prefix = self.frame.add_prefix('foo#')
expected = pd.Index(['foo#%s' % c for c in self.frame.columns])
tm.assert_index_equal(with_prefix.columns, expected)
with tm.assert_produces_warning(FutureWarning):
with_prefix = self.frame.add_prefix('foo#')
expected = pd.Index(['foo#%s' % c for c in self.frame.columns])
tm.assert_index_equal(with_prefix.columns, expected)

with_suffix = self.frame.add_suffix('#foo')
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
tm.assert_index_equal(with_suffix.columns, expected)
with tm.assert_produces_warning(FutureWarning):
with_suffix = self.frame.add_suffix('#foo')
expected = pd.Index(['%s#foo' % c for c in self.frame.columns])
tm.assert_index_equal(with_suffix.columns, expected)

with_pct_prefix = self.frame.add_prefix('%')
expected = pd.Index(['%{}'.format(c) for c in self.frame.columns])
tm.assert_index_equal(with_pct_prefix.columns, expected)
with tm.assert_produces_warning(FutureWarning):
with_pct_prefix = self.frame.add_prefix('%')
expected = pd.Index(['%{}'.format(c) for c in self.frame.columns])
tm.assert_index_equal(with_pct_prefix.columns, expected)

with_pct_suffix = self.frame.add_suffix('%')
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns])
tm.assert_index_equal(with_pct_suffix.columns, expected)
with tm.assert_produces_warning(FutureWarning):
with_pct_suffix = self.frame.add_suffix('%')
expected = pd.Index(['{}%'.format(c) for c in self.frame.columns])
tm.assert_index_equal(with_pct_suffix.columns, expected)

def test_get_axis(self):
f = self.frame
Expand Down