Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
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
92 changes: 83 additions & 9 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,17 +1018,91 @@ def rsplit(self, pat=None, *, n=-1, expand: bool = False):
Index([('X', ' ', '123'), ('Y', ' ', '999')], dtype='object')
"""

@Appender(
_shared_docs["str_partition"]
% {
"side": "first",
"return": "3 elements containing the string itself, followed by two "
"empty strings",
"also": "rpartition : Split the string at the last occurrence of `sep`.",
}
)
@forbid_nonstring_types(["bytes"])
def partition(self, sep: str = " ", expand: bool = True):
"""
Split the string at the first occurrence of `sep`.

This method splits the string at the first occurrence of `sep`,
and returns 3 elements containing the part before the separator,
the separator itself, and the part after the separator.
If the separator is not found, return 3 elements containing the string itself,
followed by two empty strings.

Parameters
----------
sep : str, default whitespace
String to split on.
expand : bool, default True
If True, return DataFrame/MultiIndex expanding dimensionality.
If False, return Series/Index.

Returns
-------
DataFrame/MultiIndex or Series/Index of objects
Returns appropriate type based on `expand` parameter with strings
split based on the `sep` parameter.

See Also
--------
rpartition : Split the string at the last occurrence of `sep`.
Series.str.split : Split strings around given separators.
str.partition : Standard library version.

Examples
--------
>>> s = pd.Series(
... ["Linda van der Berg", "George Pitt-Rivers"]
... ) # doctest: +SKIP
>>> s # doctest: +SKIP
0 Linda van der Berg
1 George Pitt-Rivers
dtype: object

>>> s.str.partition() # doctest: +SKIP
0 1 2
0 Linda van der Berg
1 George Pitt-Rivers

To partition by the last space instead of the first one:

>>> s.str.rpartition() # doctest: +SKIP
0 1 2
0 Linda van der Berg
1 George Pitt-Rivers

To partition by something different than a space:

>>> s.str.partition("-") # doctest: +SKIP
0 1 2
0 Linda van der Berg
1 George Pitt - Rivers

To return a Series containing tuples instead of a DataFrame:

>>> s.str.partition("-", expand=False) # doctest: +SKIP
0 (Linda van der Berg, , )
1 (George Pitt, -, Rivers)
dtype: object

Also available on indices:

>>> idx = pd.Index(["X 123", "Y 999"]) # doctest: +SKIP
>>> idx # doctest: +SKIP
Index(['X 123', 'Y 999'], dtype='object')

Which will create a MultiIndex:

>>> idx.str.partition() # doctest: +SKIP
MultiIndex([('X', ' ', '123'),
('Y', ' ', '999')],
)

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

>>> idx.str.partition(expand=False) # doctest: +SKIP
Index([('X', ' ', '123'), ('Y', ' ', '999')], dtype='object')
"""
result = self._data.array._str_partition(sep, expand)
if self._data.dtype == "category":
dtype = self._data.dtype.categories.dtype
Expand Down
Loading