Skip to content

Commit dfb2651

Browse files
BUG: Keep original name in str.cat (#21054)
Closes #21053
1 parent 8aff124 commit dfb2651

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pandas/core/strings.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2320,9 +2320,9 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
23202320
res = str_cat(data, others=others, sep=sep, na_rep=na_rep)
23212321

23222322
if isinstance(self._orig, Index):
2323-
res = Index(res)
2323+
res = Index(res, name=self._orig.name)
23242324
else: # Series
2325-
res = Series(res, index=data.index)
2325+
res = Series(res, index=data.index, name=self._orig.name)
23262326
return res
23272327

23282328
@copy(str_split)

pandas/tests/test_strings.py

+13
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,19 @@ def test_cat(self):
144144
with tm.assert_raises_regex(ValueError, rgx):
145145
strings.str_cat(one, 'three')
146146

147+
@pytest.mark.parametrize('container', [Series, Index])
148+
@pytest.mark.parametrize('other', [None, Series, Index])
149+
def test_str_cat_name(self, container, other):
150+
# https://github.com/pandas-dev/pandas/issues/21053
151+
values = ['a', 'b']
152+
if other:
153+
other = other(values)
154+
else:
155+
other = values
156+
result = container(values, name='name').str.cat(other, sep=',',
157+
join='left')
158+
assert result.name == 'name'
159+
147160
@pytest.mark.parametrize('series_or_index', ['series', 'index'])
148161
def test_str_cat(self, series_or_index):
149162
# test_cat above tests "str_cat" from ndarray to ndarray;

0 commit comments

Comments
 (0)