Skip to content

str.extractall with no match returns appropriate MultIndex #19075

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 4 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ Indexing
- Bug in indexing non-scalar value from ``Series`` having non-unique ``Index`` will return value flattened (:issue:`17610`)
- Bug in :func:`DatetimeIndex.insert` where inserting ``NaT`` into a timezone-aware index incorrectly raised (:issue:`16357`)
- Bug in ``__setitem__`` when indexing a :class:`DataFrame` with a 2-d boolean ndarray (:issue:`18582`)

- Bug in ``str.extractall`` when there were no matches empty :class:`Index` was returned instead of appropriate :class:`MultiIndex` (:issue:`19034`)

I/O
^^^
Expand Down
10 changes: 4 additions & 6 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,12 +794,10 @@ def str_extractall(arr, pat, flags=0):
result_key = tuple(subject_key + (match_i, ))
index_list.append(result_key)

if 0 < len(index_list):
from pandas import MultiIndex
index = MultiIndex.from_tuples(
index_list, names=arr.index.names + ["match"])
else:
index = None
from pandas import MultiIndex
index = MultiIndex.from_tuples(
index_list, names=arr.index.names + ["match"])

result = arr._constructor_expanddim(match_list, index=index,
columns=columns)
return result
Expand Down
32 changes: 24 additions & 8 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,28 +1072,44 @@ def test_extractall_single_group_with_quantifier(self):
e = DataFrame(['ab', 'abc', 'd', 'cd'], i)
tm.assert_frame_equal(r, e)

def test_extractall_no_matches(self):
s = Series(['a3', 'b3', 'd4c2'], name='series_name')
@pytest.mark.parametrize('data, names', [
([], (None, )),
([], ('i1', )),
([], (None, 'i2')),
([], ('i1', 'i2')),
(['a3', 'b3', 'd4c2'], (None, )),
(['a3', 'b3', 'd4c2'], ('i1', 'i2')),
(['a3', 'b3', 'd4c2'], (None, 'i2')),
(['a3', 'b3', 'd4c2'], ('i1', 'i2')),
])
def test_extractall_no_matches(self, data, names):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By test comments, @jreback meant a comment with the issue number, so like # GH-19075 just below the def test_

n = len(data)
if len(names) == 1:
i = Index(range(n), name=names[0])
else:
a = (tuple([i] * (n - 1)) for i in range(n))
i = MultiIndex.from_tuples(a, names=names)
s = Series(data, name='series_name', index=i, dtype='object')
ei = MultiIndex.from_tuples([], names=(names + ('match',)))
# one un-named group.
r = s.str.extractall('(z)')
e = DataFrame(columns=[0])
e = DataFrame(columns=[0], index=ei)
tm.assert_frame_equal(r, e)
# two un-named groups.
r = s.str.extractall('(z)(z)')
e = DataFrame(columns=[0, 1])
e = DataFrame(columns=[0, 1], index=ei)
tm.assert_frame_equal(r, e)
# one named group.
r = s.str.extractall('(?P<first>z)')
e = DataFrame(columns=["first"])
e = DataFrame(columns=["first"], index=ei)
tm.assert_frame_equal(r, e)
# two named groups.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you put a blank line before each comment; easier to read the tests then

r = s.str.extractall('(?P<first>z)(?P<second>z)')
e = DataFrame(columns=["first", "second"])
e = DataFrame(columns=["first", "second"], index=ei)
tm.assert_frame_equal(r, e)
# one named, one un-named.
r = s.str.extractall('(z)(?P<second>z)')
e = DataFrame(columns=[0,
"second"])
e = DataFrame(columns=[0, "second"], index=ei)
tm.assert_frame_equal(r, e)

def test_extractall_stringindex(self):
Expand Down