Skip to content

Commit 8549609

Browse files
committed
Merge pull request #7313 from cpcloud/fix-single-group-extract
BUG: single group series should preserve group name
2 parents 3dda298 + 5c31654 commit 8549609

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/v0.14.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ Bug Fixes
8080
- Bug in ``Float64Index`` which didn't allow duplicates (:issue:`7149`).
8181
- Bug in ``DataFrame.replace()`` where truthy values were being replaced
8282
(:issue:`7140`).
83+
- Bug in ``StringMethods.extract()`` where a single match group Series
84+
would use the matcher's name instead of the group name (:issue:`7313`).

pandas/core/strings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,9 @@ def _wrap_result(self, result):
906906
if not hasattr(result, 'ndim'):
907907
return result
908908
elif result.ndim == 1:
909+
name = getattr(result, 'name', None)
909910
return Series(result, index=self.series.index,
910-
name=self.series.name)
911+
name=name or self.series.name)
911912
else:
912913
assert result.ndim < 3
913914
return DataFrame(result, index=self.series.index)

pandas/tests/test_strings.py

+7
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,13 @@ def check_index(index):
578578
tm.makeDateIndex, tm.makePeriodIndex ]:
579579
check_index(index())
580580

581+
def test_extract_single_series_name_is_preserved(self):
582+
s = Series(['a3', 'b3', 'c2'], name='bob')
583+
r = s.str.extract(r'(?P<sue>[a-z])')
584+
e = Series(['a', 'b', 'c'], name='sue')
585+
tm.assert_series_equal(r, e)
586+
self.assertEqual(r.name, e.name)
587+
581588
def test_get_dummies(self):
582589
s = Series(['a|b', 'a|c', np.nan])
583590
result = s.str.get_dummies('|')

0 commit comments

Comments
 (0)