Skip to content

Commit d577d5d

Browse files
DOC: fix rsplit doc (pandas-dev#46835)
* fix rsplit doc * split out examples Co-authored-by: Marco Gorelli <[email protected]>
1 parent 4e36374 commit d577d5d

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

pandas/core/strings/accessor.py

+51-28
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ def cat(
659659
660660
Parameters
661661
----------
662-
pat : str or compiled regex, optional
663-
String or regular expression to split on.
662+
pat : str%(pat_regex)s, optional
663+
%(pat_description)s.
664664
If not specified, split on whitespace.
665665
n : int, default -1 (all)
666666
Limit number of splits in output.
@@ -670,28 +670,12 @@ def cat(
670670
671671
- If ``True``, return DataFrame/MultiIndex expanding dimensionality.
672672
- If ``False``, return Series/Index, containing lists of strings.
673-
674-
regex : bool, default None
675-
Determines if the passed-in pattern is a regular expression:
676-
677-
- If ``True``, assumes the passed-in pattern is a regular expression
678-
- If ``False``, treats the pattern as a literal string.
679-
- If ``None`` and `pat` length is 1, treats `pat` as a literal string.
680-
- If ``None`` and `pat` length is not 1, treats `pat` as a regular expression.
681-
- Cannot be set to False if `pat` is a compiled regex
682-
683-
.. versionadded:: 1.4.0
684-
673+
%(regex_argument)s
685674
Returns
686675
-------
687676
Series, Index, DataFrame or MultiIndex
688677
Type matches caller unless ``expand=True`` (see Notes).
689-
690-
Raises
691-
------
692-
ValueError
693-
* if `regex` is False and `pat` is a compiled regex
694-
678+
%(raises_split)s
695679
See Also
696680
--------
697681
Series.str.split : Split strings around given separator/delimiter.
@@ -713,10 +697,7 @@ def cat(
713697
714698
If using ``expand=True``, Series and Index callers return DataFrame and
715699
MultiIndex objects, respectively.
716-
717-
Use of `regex=False` with a `pat` as a compiled regex will raise
718-
an error.
719-
700+
%(regex_pat_note)s
720701
Examples
721702
--------
722703
>>> s = pd.Series(
@@ -790,7 +771,37 @@ def cat(
790771
0 this is a regular sentence None
791772
1 https://docs.python.org/3/tutorial index.html
792773
2 NaN NaN
774+
%(regex_examples)s"""
775+
776+
@Appender(
777+
_shared_docs["str_split"]
778+
% {
779+
"side": "beginning",
780+
"pat_regex": " or compiled regex",
781+
"pat_description": "String or regular expression to split on",
782+
"regex_argument": """
783+
regex : bool, default None
784+
Determines if the passed-in pattern is a regular expression:
785+
786+
- If ``True``, assumes the passed-in pattern is a regular expression
787+
- If ``False``, treats the pattern as a literal string.
788+
- If ``None`` and `pat` length is 1, treats `pat` as a literal string.
789+
- If ``None`` and `pat` length is not 1, treats `pat` as a regular expression.
790+
- Cannot be set to False if `pat` is a compiled regex
793791
792+
.. versionadded:: 1.4.0
793+
""",
794+
"raises_split": """
795+
Raises
796+
------
797+
ValueError
798+
* if `regex` is False and `pat` is a compiled regex
799+
""",
800+
"regex_pat_note": """
801+
Use of `regex =False` with a `pat` as a compiled regex will raise an error.
802+
""",
803+
"method": "split",
804+
"regex_examples": r"""
794805
Remember to escape special characters when explicitly using regular expressions.
795806
796807
>>> s = pd.Series(["foo and bar plus baz"])
@@ -829,9 +840,9 @@ def cat(
829840
>>> s.str.split(r"\.jpg", regex=False, expand=True)
830841
0
831842
0 foojpgbar.jpg
832-
"""
833-
834-
@Appender(_shared_docs["str_split"] % {"side": "beginning", "method": "split"})
843+
""",
844+
}
845+
)
835846
@forbid_nonstring_types(["bytes"])
836847
def split(
837848
self,
@@ -850,7 +861,19 @@ def split(
850861
result = self._data.array._str_split(pat, n, expand, regex)
851862
return self._wrap_result(result, returns_string=expand, expand=expand)
852863

853-
@Appender(_shared_docs["str_split"] % {"side": "end", "method": "rsplit"})
864+
@Appender(
865+
_shared_docs["str_split"]
866+
% {
867+
"side": "end",
868+
"pat_regex": "",
869+
"pat_description": "String to split on",
870+
"regex_argument": "",
871+
"raises_split": "",
872+
"regex_pat_note": "",
873+
"method": "rsplit",
874+
"regex_examples": "",
875+
}
876+
)
854877
@forbid_nonstring_types(["bytes"])
855878
def rsplit(self, pat=None, n=-1, expand=False):
856879
result = self._data.array._str_rsplit(pat, n=n)

0 commit comments

Comments
 (0)