Skip to content

Commit d095ac8

Browse files
authored
DOC: Fix examples in pandas/core/strings.py (#33328)
1 parent 4124dc7 commit d095ac8

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

ci/code_checks.sh

+4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
296296
pytest -q --doctest-modules pandas/core/series.py
297297
RET=$(($RET + $?)) ; echo $MSG "DONE"
298298

299+
MSG='Doctests strings.py' ; echo $MSG
300+
pytest -q --doctest-modules pandas/core/strings.py
301+
RET=$(($RET + $?)) ; echo $MSG "DONE"
302+
299303
# Directories
300304

301305
MSG='Doctests arrays'; echo $MSG

pandas/core/strings.py

+32-19
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,9 @@ def str_replace(arr, pat, repl, n=-1, case=None, flags=0, regex=True):
652652
To get the idea:
653653
654654
>>> pd.Series(['foo', 'fuz', np.nan]).str.replace('f', repr)
655-
0 <_sre.SRE_Match object; span=(0, 1), match='f'>oo
656-
1 <_sre.SRE_Match object; span=(0, 1), match='f'>uz
657-
2 NaN
655+
0 <re.Match object; span=(0, 1), match='f'>oo
656+
1 <re.Match object; span=(0, 1), match='f'>uz
657+
2 NaN
658658
dtype: object
659659
660660
Reverse every lowercase alphabetic word:
@@ -2076,8 +2076,18 @@ class StringMethods(NoNewAttributesMixin):
20762076
20772077
Examples
20782078
--------
2079-
>>> s.str.split('_')
2080-
>>> s.str.replace('_', '')
2079+
>>> s = pd.Series(["A_Str_Series"])
2080+
>>> s
2081+
0 A_Str_Series
2082+
dtype: object
2083+
2084+
>>> s.str.split("_")
2085+
0 [A, Str, Series]
2086+
dtype: object
2087+
2088+
>>> s.str.replace("_", "")
2089+
0 AStrSeries
2090+
dtype: object
20812091
"""
20822092

20832093
def __init__(self, data):
@@ -2583,9 +2593,14 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
25832593
25842594
Examples
25852595
--------
2586-
>>> s = pd.Series(["this is a regular sentence",
2587-
... "https://docs.python.org/3/tutorial/index.html",
2588-
... np.nan])
2596+
>>> s = pd.Series(
2597+
... [
2598+
... "this is a regular sentence",
2599+
... "https://docs.python.org/3/tutorial/index.html",
2600+
... np.nan
2601+
... ]
2602+
... )
2603+
>>> s
25892604
0 this is a regular sentence
25902605
1 https://docs.python.org/3/tutorial/index.html
25912606
2 NaN
@@ -2625,7 +2640,7 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
26252640
26262641
The `pat` parameter can be used to split by other characters.
26272642
2628-
>>> s.str.split(pat = "/")
2643+
>>> s.str.split(pat="/")
26292644
0 [this is a regular sentence]
26302645
1 [https:, , docs.python.org, 3, tutorial, index...
26312646
2 NaN
@@ -2636,14 +2651,10 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
26362651
the columns during the split.
26372652
26382653
>>> s.str.split(expand=True)
2639-
0 1 2 3
2640-
0 this is a regular
2641-
1 https://docs.python.org/3/tutorial/index.html None None None
2642-
2 NaN NaN NaN NaN \
2643-
4
2644-
0 sentence
2645-
1 None
2646-
2 NaN
2654+
0 1 2 3 4
2655+
0 this is a regular sentence
2656+
1 https://docs.python.org/3/tutorial/index.html None None None None
2657+
2 NaN NaN NaN NaN NaN
26472658
26482659
For slightly more complex use cases like splitting the html document name
26492660
from a url, a combination of parameter settings can be used.
@@ -2658,7 +2669,9 @@ def cat(self, others=None, sep=None, na_rep=None, join="left"):
26582669
expressions.
26592670
26602671
>>> s = pd.Series(["1+1=2"])
2661-
2672+
>>> s
2673+
0 1+1=2
2674+
dtype: object
26622675
>>> s.str.split(r"\+|=", expand=True)
26632676
0 1 2
26642677
0 1 1 2
@@ -2750,7 +2763,7 @@ def rsplit(self, pat=None, n=-1, expand=False):
27502763
>>> idx.str.partition()
27512764
MultiIndex([('X', ' ', '123'),
27522765
('Y', ' ', '999')],
2753-
dtype='object')
2766+
)
27542767
27552768
Or an index with tuples with ``expand=False``:
27562769

0 commit comments

Comments
 (0)