-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: improve extract and get_dummies methods for Index.str (fix for #9980) #9985
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -516,7 +516,6 @@ def test_match(self): | |
|
||
def test_extract(self): | ||
# Contains tests like those in test_match and some others. | ||
|
||
values = Series(['fooBAD__barBAD', NA, 'foo']) | ||
er = [NA, NA] # empty row | ||
|
||
|
@@ -540,15 +539,31 @@ def test_extract(self): | |
exp = DataFrame([[u('BAD__'), u('BAD')], er, er]) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# no groups | ||
s = Series(['A1', 'B2', 'C3']) | ||
f = lambda: s.str.extract('[ABC][123]') | ||
self.assertRaises(ValueError, f) | ||
|
||
# only non-capturing groups | ||
f = lambda: s.str.extract('(?:[AB]).*') | ||
self.assertRaises(ValueError, f) | ||
# GH9980 | ||
# Index only works with one regex group since | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add the issue reference here as a comment |
||
# multi-group would expand to a frame | ||
idx = Index(['A1', 'A2', 'A3', 'A4', 'B5']) | ||
with tm.assertRaisesRegexp(ValueError, "supported"): | ||
idx.str.extract('([AB])([123])') | ||
|
||
# these should work for both Series and Index | ||
for klass in [Series, Index]: | ||
# no groups | ||
s_or_idx = klass(['A1', 'B2', 'C3']) | ||
f = lambda: s_or_idx.str.extract('[ABC][123]') | ||
self.assertRaises(ValueError, f) | ||
|
||
# only non-capturing groups | ||
f = lambda: s_or_idx.str.extract('(?:[AB]).*') | ||
self.assertRaises(ValueError, f) | ||
|
||
# single group renames series/index properly | ||
s_or_idx = klass(['A1', 'A2']) | ||
result = s_or_idx.str.extract(r'(?P<uno>A)\d') | ||
tm.assert_equal(result.name, 'uno') | ||
tm.assert_array_equal(result, klass(['A', 'A'])) | ||
|
||
s = Series(['A1', 'B2', 'C3']) | ||
# one group, no matches | ||
result = s.str.extract('(_)') | ||
exp = Series([NA, NA, NA], dtype=object) | ||
|
@@ -569,14 +584,16 @@ def test_extract(self): | |
exp = DataFrame([['A', '1'], ['B', '2'], [NA, NA]]) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# named group/groups | ||
result = s.str.extract('(?P<letter>[AB])(?P<number>[123])') | ||
exp = DataFrame([['A', '1'], ['B', '2'], [NA, NA]], columns=['letter', 'number']) | ||
tm.assert_frame_equal(result, exp) | ||
# one named group | ||
result = s.str.extract('(?P<letter>[AB])') | ||
exp = Series(['A', 'B', NA], name='letter') | ||
tm.assert_series_equal(result, exp) | ||
|
||
# two named groups | ||
result = s.str.extract('(?P<letter>[AB])(?P<number>[123])') | ||
exp = DataFrame([['A', '1'], ['B', '2'], [NA, NA]], columns=['letter', 'number']) | ||
tm.assert_frame_equal(result, exp) | ||
|
||
# mix named and unnamed groups | ||
result = s.str.extract('([AB])(?P<number>[123])') | ||
exp = DataFrame([['A', '1'], ['B', '2'], [NA, NA]], columns=[0, 'number']) | ||
|
@@ -602,11 +619,6 @@ 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): | ||
|
@@ -761,6 +773,12 @@ def test_get_dummies(self): | |
columns=list('7ab')) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# GH9980 | ||
# Index.str does not support get_dummies() as it returns a frame | ||
with tm.assertRaisesRegexp(TypeError, "not supported"): | ||
idx = Index(['a|b', 'a|c', 'b|c']) | ||
idx.str.get_dummies('|') | ||
|
||
def test_join(self): | ||
values = Series(['a_b_c', 'c_d_e', np.nan, 'f_g_h']) | ||
result = values.str.split('_').str.join('_') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
similarly, it might be nice to instead just extend wrap?
also, I recall discussing returning a MultiIndex for these cases instead of raising.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think returning a
MultiIndex
is a nice idea. But let's defer that to 0.17.0 (if someone can find/link the issue would br gr8).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shoyer I can't quite see a clean way to modify
_wrap_result
for this, we would need to pass down extra params likeregex.groups
and regex name, might become harder to understand?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback @shoyer Returning a
MultiIndex
is definitely a good idea, I think it was proposed here in a conversation thread #9870 (comment) but no issue has been created yet. I'll create one and link it back