-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5e780e9
DOC: Improve the docstring of Series.add_prefix()
astrastefania 397f05d
DOC: Fixe the issues in the comments
astrastefania c719d7a
DOC: Update panle to Series
astrastefania e14315d
DOC: Added DataFrame example
astrastefania f23e340
DOC: Fix PEP8
astrastefania 177285d
DOC: Fix PEP8spaces
astrastefania 67c8dbe
DOC: Fix last docstring issue
astrastefania 6d599ad
Spacing
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a blank line and then an example with |
||
|
||
>>> 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) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 useSeries 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
I want to avoid
items
, as (to me) I thinkdict.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?
There was a problem hiding this comment.
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. :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 bothSeries.add_prefix
andDataFrame.add_prefix
.