-
-
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
Changes from 6 commits
5e780e9
397f05d
c719d7a
e14315d
f23e340
177285d
67c8dbe
6d599ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2963,15 +2963,54 @@ 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 | ||
Original Series or DataFrame with updated labels. | ||
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. This make it sound a bit like the object is modified inplace. Could you instead say
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. Actually is modified inplace, should it be specified? 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. Hmm I don't think so In [6]: s = pd.Series(1, ['a', 'b'])
In [7]: s
Out[7]:
a 1
b 1
dtype: int64
In [8]: s.add_prefix('foo_')
Out[8]:
foo_a 1
foo_b 1
dtype: int64
In [9]: s
Out[9]:
a 1
b 1
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. Sorry, I meant it's unmodified! I agree is not modified inplace. |
||
|
||
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_suffix('_item') | ||
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. This should be the 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. Yes!!! 😄 |
||
A_item B_item | ||
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) | ||
|
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
.