Skip to content

DOC: Clarify str.cat output for Index object (GH35556) #61833

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
36 changes: 24 additions & 12 deletions pandas/core/strings/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,12 @@ def cat(
to match the length of the calling Series/Index). To disable
alignment, use `.values` on any Series/Index/DataFrame in `others`.

Returns
-------
str, Series or Index
If `others` is None, `str` is returned, otherwise a `Series/Index`
(same type as caller) of objects is returned.
Returns
-------
str, Series or Index
- If the caller is a Series and `others` is None, a single concatenated `str` is returned.
- If the caller is an Index and `others` is None, an Index of length 1 containing the concatenated string is returned.
- If `others` is specified, the result is a Series or Index (same type as caller) of concatenated strings.

See Also
--------
Expand Down Expand Up @@ -608,14 +609,25 @@ def cat(
dtype: object
>>>
>>> s.str.cat(t, join="right", na_rep="-")
3 dd
0 aa
4 -e
2 -c
dtype: object
3 dd
0 aa
4 -e
2 -c
dtype: object

When calling `.str.cat()` on an Index and not passing `others`, the return value is an Index:

>>> idx = pd.Index(["x", "y", np.nan])
>>> idx.str.cat(sep="-")
Index(['x-y'], dtype='object')

Note
----
Calling `.str.cat()` on a Series returns a string if `others` is None,
but when called on an Index, it returns a new Index containing the concatenated string.

For more examples, see :ref:`here <text.concatenate>`.

For more examples, see :ref:`here <text.concatenate>`.
"""
# TODO: dispatch
from pandas import (
Index,
Expand Down
Loading