You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The parameter ``others`` can also be two-dimensional. In this case, the number or rows must match the lengths of the calling ``Series`` (or ``Index``).
251
256
252
257
.. ipython:: python
253
258
254
-
u = pd.Series(['b', 'd', 'e', 'c'], index=[1, 3, 4, 2])
255
-
# without alignment
259
+
d = pd.concat([t, s], axis=1)
260
+
d
261
+
s.str.cat(d, na_rep='-')
262
+
263
+
Concatenating a Series and an indexed object into a Series, with alignment
For concatenation with a ``Series`` or ``DataFrame``, it is possible to align the respective indexes before concatenation by setting
269
+
the ``join``-keyword, which controls the manner of alignment.
270
+
271
+
.. ipython:: python
272
+
273
+
u = pd.Series(['b', 'd', 'a', 'c'], index=[1, 3, 0, 2])
256
274
s.str.cat(u)
257
-
# with separate alignment
258
-
v, w = s.align(u)
259
-
v.str.cat(w, na_rep='-')
275
+
s.str.cat(u, join='left')
276
+
277
+
.. warning::
278
+
279
+
If the ``join`` keyword is not passed, the method :meth:`~Series.str.cat` will currently fall back to the behavior before version 0.23.0 (i.e. no alignment),
280
+
but a ``FutureWarning`` will be raised, since this default will change to ``join='left'`` in a future version.
281
+
282
+
To usual options are available for ``join`` (one of ``'left', 'outer', 'inner', 'right'``).
283
+
In particular, alignment also means that the different lengths do not need to coincide anymore.
Copy file name to clipboardExpand all lines: doc/source/whatsnew/v0.23.0.txt
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -308,6 +308,39 @@ The :func:`DataFrame.assign` now accepts dependent keyword arguments for python
308
308
309
309
df.assign(A=df.A+1, C= lambda df: df.A* -1)
310
310
311
+
.. _whatsnew_0230.enhancements.str_cat_align:
312
+
313
+
``Series.str.cat`` has gained the ``join`` kwarg
314
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
315
+
316
+
Previously, :meth:`Series.str.cat` did not -- in contrast to most of ``pandas`` -- align :class:`Series` on their index before concatenation (see :issue:`18657`).
317
+
The method has now gained a keyword ``join`` to control the manner of alignment. In v.0.23 it will default to None (meaning no alignment), but this default will change
318
+
to ``'left'`` in a future version of pandas.
319
+
320
+
.. ipython:: python
321
+
322
+
s = pd.Series(['a', 'b', 'c', 'd'])
323
+
t = pd.Series(['b', 'd', 'e', 'c'], index=[1, 3, 4, 2])
324
+
s.str.cat(t)
325
+
s.str.cat(t, join='left', na_rep='-')
326
+
327
+
In particular, ``others`` does not need to be of the same length as the calling ``Series`` (if both have an index and ``join is not None``).
328
+
For more examples, see :ref:`here <text.concatenate>`.
329
+
330
+
Additionally, ``str.cat`` now allows ``others`` to be a ``DataFrame`` or two-dimensional ``np.ndarray``.
331
+
332
+
.. ipython:: python
333
+
334
+
u = pd.Series(['b', 'd', 'a', 'c'], index=[1, 3, 0, 2])
335
+
d = pd.concat([s, u], axis=1)
336
+
t.str.cat(d.values)
337
+
s.str.cat(d, join='left', na_rep='-')
338
+
339
+
Furthermore, any combination of "concatenateable" arguments can be passed in a list-like container (e.g. an iterator).
340
+
341
+
For categorical data, it is now possible to call :meth:`Series.str.cat` for ``CategoricalIndex`` as well (previously raised a ``ValueError``).
342
+
Finally, if ``others is not None``, the resulting ``Series``/``Index`` will now remain categorical if the calling
0 commit comments