Skip to content

BUG: properly rename single group match in Series.str.extract() #6888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ Bug Fixes
- Bug in ``groupby.get_group`` where a datetlike wasn't always accepted (:issue:`5267`)
- Bug in ``DatetimeIndex.tz_localize`` and ``DatetimeIndex.tz_convert`` affects to NaT (:issue:`5546`)
- Bug in arithmetic operations affecting to NaT (:issue:`6873`)
- Bug in ``Series.str.extract`` where the resulting ``Series`` from a single
group match wasn't renamed to the group name

pandas 0.13.1
-------------
Expand Down
9 changes: 8 additions & 1 deletion pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ def f(x):
return _na_map(f, arr, na)


def _get_single_group_name(rx):
try:
return list(rx.groupindex.keys()).pop()
except IndexError:
return None


def str_extract(arr, pat, flags=0):
"""
Find groups in each string using passed regular expression
Expand Down Expand Up @@ -452,7 +459,7 @@ def f(x):
return empty_row
if regex.groups == 1:
result = Series([f(val)[0] for val in arr],
name=regex.groupindex.get(1),
name=_get_single_group_name(regex),
index=arr.index)
else:
names = dict(zip(regex.groupindex.values(), regex.groupindex.keys()))
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ def test_extract(self):
exp = DataFrame([['A', '1'], ['B', '2'], ['C', NA]], columns=['letter', 'number'])
tm.assert_frame_equal(result, exp)

# single group renames series properly
s = Series(['A1', 'A2'])
result = s.str.extract(r'(?P<uno>A)\d')
tm.assert_equal(result.name, 'uno')

# GH6348
# not passing index to the extractor
def check_index(index):
Expand Down