Skip to content

DOC: Fix examples in pandas/core/strings.py #33328

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
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
pytest -q --doctest-modules pandas/core/series.py
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Doctests strings.py' ; echo $MSG
pytest -q --doctest-modules pandas/core/strings.py
RET=$(($RET + $?)) ; echo $MSG "DONE"

# Directories

MSG='Doctests arrays'; echo $MSG
Expand Down
51 changes: 32 additions & 19 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,9 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=True):
To get the idea:

>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
0 <_sre.SRE_Match object; span=(0, 1), match='f'>oo
1 <_sre.SRE_Match object; span=(0, 1), match='f'>uz
2 NaN
0 <re.Match object; span=(0, 1), match='f'>oo
1 <re.Match object; span=(0, 1), match='f'>uz
2 NaN
dtype: object

Reverse every lowercase alphabetic word:
Expand Down Expand Up @@ -2076,8 +2076,18 @@ class StringMethods(NoNewAttributesMixin):

Examples
--------
>>> s.str.split('_')
>>> s.str.replace('_', '')
>>> s = pd.Series(["A_Str_Series"])
>>> s
0 A_Str_Series
dtype: object

>>> s.str.split("_")
0 [A, Str, Series]
dtype: object

>>> s.str.replace("_", "")
0 AStrSeries
dtype: object
"""

def __init__(self, data):
Expand Down Expand Up @@ -2583,9 +2593,14 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):

Examples
--------
>>> s = pd.Series(["this is a regular sentence",
... "https://docs.python.org/3/tutorial/index.html",
... np.nan])
>>> s = pd.Series(
... [
... "this is a regular sentence",
... "https://docs.python.org/3/tutorial/index.html",
... np.nan
... ]
... )
>>> s
0 this is a regular sentence
1 https://docs.python.org/3/tutorial/index.html
2 NaN
Expand Down Expand Up @@ -2625,7 +2640,7 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):

The `pat` parameter can be used to split by other characters.

>>> s.str.split(pat = "/")
>>> s.str.split(pat="/")
0 [this is a regular sentence]
1 [https:, , docs.python.org, 3, tutorial, index...
2 NaN
Expand All @@ -2636,14 +2651,10 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
the columns during the split.

>>> s.str.split(expand=True)
0 1 2 3
0 this is a regular
1 https://docs.python.org/3/tutorial/index.html None None None
2 NaN NaN NaN NaN \
4
0 sentence
1 None
2 NaN
0 1 2 3 4
0 this is a regular sentence
1 https://docs.python.org/3/tutorial/index.html None None None None
2 NaN NaN NaN NaN NaN

For slightly more complex use cases like splitting the html document name
from a url, a combination of parameter settings can be used.
Expand All @@ -2658,7 +2669,9 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
expressions.

>>> s = pd.Series(["1+1=2"])

>>> s
0 1+1=2
dtype: object
>>> s.str.split(r"\+|=", expand=True)
0 1 2
0 1 1 2
Expand Down Expand Up @@ -2750,7 +2763,7 @@ def rsplit(self, pat=None, n=-1, expand=False):
>>> idx.str.partition()
MultiIndex([('X', ' ', '123'),
('Y', ' ', '999')],
dtype='object')
)

Or an index with tuples with ``expand=False``:

Expand Down