Skip to content

DOC: update docstring of pandas.Series.add_prefix docstring #20313

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

Merged
merged 8 commits into from
Mar 13, 2018
Merged
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
47 changes: 44 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2963,15 +2963,56 @@ def _update_inplace(self, result, verify_is_copy=True):

def add_prefix(self, prefix):
"""
Concatenate prefix string with panel items names.
Prefix labels with string `prefix`.

For Series, the row labels are prefixed.
For DataFrame, the column labels are prefixed.

Copy link
Member

Choose a reason for hiding this comment

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

Panel is deprecated, we may want to use Series or DataFrame, or something generic.

I'd try to find uses cases on the internet for this method, and add in the extended summary when this method can be useful, if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I used Series as it is a Series module in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

For this and #20315, these apply to both Series and DataFrame (and Panel, but we don't care about that). So perhaps

Prefix row labels with a string `prefix`.

I want to avoid items, as (to me) I think dict.items so key-value pairs. But we're just touching the row labels here.

That summary is strange since we use Prefix as a verb and nound. Maybe "Prepend" would be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, thank you, my doubt is that on DataFrames add_prefix() adds a prefix on columns names not on rows, for example: df = pd.DataFrame({'A':[1,2,3,4], 'B':[3,4,5,6]}), df.add_suffix('_item'). For this motivations I decided to leave on Series.

Perhaps I could updated the docstring for pandas.DataFrame.add_prefix with the relative example to avoid confusion and let this as it is.

Let me know what sound better. :)

Copy link
Contributor

@TomAugspurger TomAugspurger Mar 13, 2018

Choose a reason for hiding this comment

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

Ahh, good catch! I would say splitting the docstring is more complexity that warranted. How about somehting like

"""
Prefix labels with string `prefix`.

For Series, the row labels are prefixed. For DataFrame, the column labels are prefixed

...
"""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I do agree, thanks!

I didn't know about the issue of splitting docstring, so in this case is better have the same for both pandas.Series.add_prefix and pandas.DataFrame.add_prefix?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, the docstring you've been editing in core/generic.py is used by both Series.add_prefix and DataFrame.add_prefix.

Parameters
----------
prefix : string
prefix : str
The string to add before each label.

Returns
-------
with_prefix : type of caller
Series or DataFrame
New Series or DataFrame with updated labels.

See Also
--------
Series.add_suffix: Suffix labels with string `suffix`.

Examples
--------
>>> s = pd.Series([1, 2, 3, 4])
>>> s
0 1
1 2
2 3
3 4
dtype: int64

>>> s.add_prefix('item_')
item_0 1
item_1 2
item_2 3
item_3 4
dtype: int64
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a blank line and then an example with DataFrame.


>>> df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [3, 4, 5, 6]})
>>> df
A B
0 1 3
1 2 4
2 3 5
3 4 6

>>> df.add_prefix('col_')
col_A col_B
0 1 3
1 2 4
2 3 5
3 4 6
"""
new_data = self._data.add_prefix(prefix)
return self._constructor(new_data).__finalize__(self)
Expand Down