Skip to content

Commit 2298503

Browse files
committed
Fixed issue 18657
1 parent c857b4f commit 2298503

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

pandas/core/strings.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,23 @@
3535
_shared_docs = dict()
3636

3737

38-
def _get_array_list(arr, others):
38+
def _get_array_list(arr, others, align=True):
3939
from pandas.core.series import Series
40-
41-
if len(others) and isinstance(com._values_from_object(others)[0],
40+
41+
if (len(arr) and isinstance(arr, Series)
42+
and len(others) and isinstance(others, Series)):
43+
if align is None:
44+
align = False
45+
warnings.warn("A future version of pandas will perform alignment "
46+
"when others is a series. To disable alignment (the "
47+
"previous behavior) and silence this warning, pass "
48+
"'align=False'. To enable alignment (the future "
49+
"behavior) and silence this warning, pass "
50+
"'align=True'")
51+
if align:
52+
arr, others = arr.align(others, join = 'left')
53+
arrays = [list(arr), list(others)]
54+
elif len(others) and isinstance(com._values_from_object(others)[0],
4255
(list, np.ndarray, Series)):
4356
arrays = [arr] + list(others)
4457
else:
@@ -47,7 +60,7 @@ def _get_array_list(arr, others):
4760
return [np.asarray(x, dtype=object) for x in arrays]
4861

4962

50-
def str_cat(arr, others=None, sep=None, na_rep=None):
63+
def str_cat(arr, others=None, sep=None, na_rep=None, align=None):
5164
"""
5265
Concatenate strings in the Series/Index with given separator.
5366
@@ -109,7 +122,7 @@ def str_cat(arr, others=None, sep=None, na_rep=None):
109122
sep = ''
110123

111124
if others is not None:
112-
arrays = _get_array_list(arr, others)
125+
arrays = _get_array_list(arr, others, align=align)
113126

114127
n = _length_check(arrays)
115128
masks = np.array([isna(x) for x in arrays])
@@ -1737,9 +1750,10 @@ def cons_row(x):
17371750
return cons(result, name=name, index=index)
17381751

17391752
@copy(str_cat)
1740-
def cat(self, others=None, sep=None, na_rep=None):
1753+
def cat(self, others=None, sep=None, na_rep=None, align=None):
17411754
data = self._orig if self._is_categorical else self._data
1742-
result = str_cat(data, others=others, sep=sep, na_rep=na_rep)
1755+
result = str_cat(data, others=others, sep=sep,
1756+
na_rep=na_rep, align=align)
17431757
return self._wrap_result(result, use_codes=(not self._is_categorical))
17441758

17451759
@copy(str_split)

pandas/tests/test_strings.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,11 +2743,12 @@ def test_cat_on_filtered_index(self):
27432743

27442744
str_year = df.year.astype('str')
27452745
str_month = df.month.astype('str')
2746-
str_both = str_year.str.cat(str_month, sep=' ')
2746+
str_both = str_year.str.cat(str_month, sep=' ', align=True)
27472747

27482748
assert str_both.loc[1] == '2011 2'
27492749

2750-
str_multiple = str_year.str.cat([str_month, str_month], sep=' ')
2750+
str_multiple = str_year.str.cat([str_month, str_month],
2751+
sep=' ', align=True)
27512752

27522753
assert str_multiple.loc[1] == '2011 2 2'
27532754

@@ -2759,6 +2760,18 @@ def test_str_cat_raises_intuitive_error(self):
27592760
s.str.cat('|')
27602761
with tm.assert_raises_regex(ValueError, message):
27612762
s.str.cat(' ')
2763+
2764+
def test_str_cat_align(self):
2765+
# https://github.com/pandas-dev/pandas/issues/18657
2766+
s = Series(['a', 'b', 'c', 'd'])
2767+
t = Series(['a', 'b', 'c', 'd']).reindex([1,3,0,2])
2768+
expect_aligned = Series(['aa', 'bb', 'cc', 'dd'])
2769+
expect_unaligned = Series(['ab', 'bd', 'ca', 'dc'])
2770+
with tm.assert_produces_warning(expected_warning=UserWarning):
2771+
tm.assert_series_equal(s.str.cat(t), expect_unaligned)
2772+
tm.assert_series_equal(s.str.cat(t, align=False), expect_unaligned)
2773+
tm.assert_series_equal(s.str.cat(t, align=True), expect_aligned)
2774+
27622775

27632776
def test_index_str_accessor_visibility(self):
27642777
from pandas.core.strings import StringMethods
@@ -2819,9 +2832,9 @@ def test_method_on_bytes(self):
28192832
lhs = Series(np.array(list('abc'), 'S1').astype(object))
28202833
rhs = Series(np.array(list('def'), 'S1').astype(object))
28212834
if compat.PY3:
2822-
pytest.raises(TypeError, lhs.str.cat, rhs)
2835+
pytest.raises(TypeError, lhs.str.cat, rhs, align=True)
28232836
else:
2824-
result = lhs.str.cat(rhs)
2837+
result = lhs.str.cat(rhs, align=True)
28252838
expected = Series(np.array(
28262839
['ad', 'be', 'cf'], 'S2').astype(object))
28272840
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)