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
48 changes: 27 additions & 21 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2159,46 +2159,52 @@ def encode(self, encoding, errors="strict"):

Parameters
----------
to_strip : str, optional
to_strip : str or None, default None (whitespaces are removed)
Copy link
Contributor

Choose a reason for hiding this comment

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

the coment about whitespaces are removed should be on the next line with a case, e.g.
If None then whitespaces are removed

Specifying the set of characters to be removed.
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
Series/Index of objects

See Also
--------
str.slice : Slice substrings from each element in the Series/Index
Series.str.strip : Remove leading and trailing characters in Series/Index
Series.str.lstrip : Remove leading characters in Series/Index
Series.str.rstrip : Remove trailing characters in Series/Index

Examples
--------
Stripping whitespaces for Series

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

>>> s = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t'])
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a np.nan in the example (in place of an entry or in addition is ok)

>>> 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
0 1. Ant.
1 2. Bee!\n
2 3. Cat?\t
dtype: object

>>> s.str.strip()
0 ant
1 bee
2 cat
0 1. Ant.
1 2. Bee!
2 3. Cat?
dtype: object

Stripping a set of characters for Index

>>> idx = pd.Index(['1.ant ','2._bee__','3. cat_'])
>>> s.str.strip('123.!? \n\t')
Copy link
Member

Choose a reason for hiding this comment

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

Personally I find this example overcomplicated. I think something more realistic and simple could be:

  • Illustrate the default to_strip parameter (whitespaces) with rstrip in a value like ` text \n'
  • lstrip(to_strip='0') for a value like 0010010)
  • And then you can show strip(to_strip=' \n0') with both values

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 still wants to keep the 'Animal' theme in the example but I will simplify the lstrip and rstrip example

0 Ant
1 Bee
2 Cat
dtype: object

>>> idx
Index(['1.ant ', '2._bee__', '3. cat_'], dtype='object')
>>> s.str.lstrip('123.!? \n\t')
0 Ant.
1 Bee!\n
2 Cat?\t
dtype: object

>>> idx.str.strip('123._ ')
Index(['ant', 'bee', 'cat'], dtype='object')
>>> s.str.rstrip('123.!? \n\t')
0 1. Ant
1 2. Bee
2 3. Cat
dtype: object
""")

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