Skip to content

Commit ec2e891

Browse files
clarify .str.cat behavior with Index/Series
1 parent a7d3558 commit ec2e891

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pandas/core/strings/accessor.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,13 @@ def cat(
527527
alignment, use `.values` on any Series/Index/DataFrame in `others`.
528528
529529
Returns
530+
Notes
531+
-----
532+
When concatenating with a Series or Index, pandas aligns on index labels
533+
by default. This can produce NaN values if the indices do not match.
534+
To get element-wise concatenation (the behavior before v0.23),
535+
convert the object to numpy arrays with ``.values`` or ``.to_numpy()``.
536+
530537
-------
531538
str, Series or Index
532539
If `others` is None, `str` is returned, otherwise a `Series/Index`
@@ -613,6 +620,23 @@ def cat(
613620
4 -e
614621
2 -c
615622
dtype: str
623+
Examples with Index
624+
-------------------
625+
>>> idx = pd.Index(["a", "b", "c"])
626+
>>> ser = pd.Series(["x", "y", "z"])
627+
628+
# Default alignment behavior (indices match here)
629+
>>> idx.str.cat(ser, join="left")
630+
Index(['ax', 'by', 'cz'], dtype='object')
631+
632+
# If indices do not match, result may contain NaN
633+
>>> ser2 = pd.Series(["x", "y", "z"], index=[10, 11, 12])
634+
>>> idx.str.cat(ser2, join="left")
635+
Index([nan, nan, nan], dtype='object')
636+
637+
# Element-wise concatenation (old behavior) using .values
638+
>>> idx.str.cat(ser.values)
639+
Index(['ax', 'by', 'cz'], dtype='object')
616640
617641
For more examples, see :ref:`here <text.concatenate>`.
618642
"""

0 commit comments

Comments
 (0)