Skip to content
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

update the pandas.Series.str.repeat docstring #20634

Closed
Closed
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
38 changes: 33 additions & 5 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,17 +678,45 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=True):

def str_repeat(arr, repeats):
"""
Duplicate each string in the Series/Index by indicated number
of times.
Duplicate each string repeated by indicated number of times.

Duplicate each string in the Series/Index by indicated number of times.
Copy link
Member

Choose a reason for hiding this comment

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

This seems redundant, can we get rid of it?

A passed value of zero or negative integer will return an empty string.

Parameters
----------
repeats : int or array
Same value for all (int) or different value per (array)
repeats : int or array-like
Copy link
Member

Choose a reason for hiding this comment

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

can we use sequence instead of array-like (we use array-like to refer to objects that follow numpy ndarray api)

Same value for all (int) or different value per (array).

Returns
-------
repeated : Series/Index of objects
Series or Index
Series or Index of repeated string objects specified by
input parameter repeats.

Examples
--------
>>> s = pd.Series(['a', 'b', 'c', 'd', 'e'])
Copy link
Member

Choose a reason for hiding this comment

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

I think having 5 cases that illustrate exactly the same is not very useful. Can we have:

  • A single letter (may be two or three, for the next example to show different number of repetitions)
  • A word
  • A NaN
  • A number

Can you also show the value of s, so it's easier to compare with the result of str.repeat


Using same value for all:

>>> s.str.repeat(repeats=4)
0 aaaa
1 bbbb
2 cccc
3 dddd
4 eeee
dtype: object

Using different value per element:

>>> s.str.repeat(repeats=[-2, -1, 0, 1, 2])
0
1
2
3 d
4 ee
dtype: object
"""
if is_scalar(repeats):

Expand Down