Skip to content

DOC: update the pandas.Series.str.strip docstring #20863

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 9 commits into from
Jul 7, 2018
Merged
46 changes: 44 additions & 2 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2151,12 +2151,54 @@ def encode(self, encoding, errors="strict"):
return self._wrap_result(result)

_shared_docs['str_strip'] = ("""
Strip whitespace (including newlines) from each string in the
Series/Index from %(side)s. Equivalent to :meth:`str.%(method)s`.
Remove leading and trailing characters.

Strip whitespaces (including newlines) or a set of specified characters
from each string in the Series/Index from %(side)s.
Equivalent to :meth:`str.%(method)s`.

Parameters
----------
to_strip : str
Copy link
Member

Choose a reason for hiding this comment

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

to_strip : str, optional

Specifying the set of characters to be removed.
Copy link
Member

Choose a reason for hiding this comment

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

Let's just simplify this and say "The set of characters to be removed". It matches the standard Python string method so no need to be too wordy

All combinations of this set of characters will be stripped.
Default value is None, which means whaitspaces will be removed.

Returns
-------
stripped : Series/Index of objects
Copy link
Member

Choose a reason for hiding this comment

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

Can you get rid of the name stripped, just leave the type in that line, and in the next add a description of what is being returned?


See Also
--------
str.slice : Slice substrings from each element in the Series/Index
Copy link
Member

Choose a reason for hiding this comment

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

slice doesn't seem very related to strip.

Not sure if there is anything else related, but we should have here Series.str.strip, Series.str.lstrip and Series.str.rstrip, so they link each other.


Examples
--------
Striping whitespaces for Series
Copy link
Member

Choose a reason for hiding this comment

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

"Striping" is a typo here and below, though these examples are simple enough that I don't think you need to explain them


>>> s = pd.Series([' ant', 'bee ', ' cat '])

Copy link
Member

Choose a reason for hiding this comment

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

this blank line is unnecessary.

>>> s
Copy link
Member

Choose a reason for hiding this comment

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

This is simple enough that you don't need to print the Series as is

0 ant
1 bee
2 cat
dtype: object

>>> s.str.strip()
0 ant
1 bee
2 cat
dtype: object

Striping a set of characters for Index
Copy link
Member

Choose a reason for hiding this comment

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

Typo here, but as mentioned above these are basic enough that I personally don't think you need to explain in detail


>>> df = pd.DataFrame(index=['1.ant ','2._bee__','3. cat_'])
Copy link
Member

Choose a reason for hiding this comment

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

Just create the index directly instead of creating a DataFrame

index = pd.Index(['1.ant ']) ...


>>> df.index
Copy link
Member

Choose a reason for hiding this comment

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

Again simple enough you don't need to print

Index(['1.ant ', '2._bee__', '3. cat_'], dtype='object')

>>> df.index.str.strip('123._ ')
Index(['ant', 'bee', 'cat'], dtype='object')
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 a single example, with a single Series with multiple values that illustrate:

  • The method without parameters s.str.strip() and how it strips not only spaces but new lines and tabs
  • The characters to strip in the left, the right and both sides
  • The use of the 3 methods being documented lstrip, rstrip, rstrip
  • The use of another character to strip (something that looks like a real-world example)
  • Stripping more than a single character (also something a bit more real-world if possible would be great.

""")

@Appender(_shared_docs['str_strip'] % dict(side='left and right sides',
Expand Down