-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: add StringMethods (.str accessor) to Index, fixes #9068 #9667
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
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import pandas.tslib as tslib | ||
import pandas.lib as lib | ||
from pandas.util.decorators import Appender, cache_readonly | ||
from pandas.core.strings import StringMethods | ||
|
||
|
||
_shared_docs = dict() | ||
|
@@ -497,6 +498,24 @@ def searchsorted(self, key, side='left'): | |
#### needs tests/doc-string | ||
return self.values.searchsorted(key, side=side) | ||
|
||
# string methods | ||
def _make_str_accessor(self): | ||
from pandas.core.series import Series | ||
from pandas.core.index import Index | ||
if isinstance(self, Series) and not com.is_object_dtype(self.dtype): | ||
# this really should exclude all series with any non-string values, | ||
# but that isn't practical for performance reasons until we have a | ||
# str dtype (GH 9343) | ||
raise AttributeError("Can only use .str accessor with string " | ||
"values, which use np.object_ dtype in " | ||
"pandas") | ||
elif isinstance(self, Index) and self.inferred_type != 'string': | ||
raise AttributeError("Can only use .str accessor with string " | ||
"values (i.e. inferred_type is 'string')") | ||
return StringMethods(self) | ||
|
||
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. |
||
str = AccessorProperty(StringMethods, _make_str_accessor) | ||
|
||
_shared_docs['drop_duplicates'] = ( | ||
"""Return %(klass)s with duplicate values removed | ||
|
||
|
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import numpy as np | ||
|
||
from pandas.compat import zip | ||
from pandas.core.common import isnull, _values_from_object | ||
from pandas.core.common import isnull, _values_from_object, is_bool_dtype | ||
import pandas.compat as compat | ||
from pandas.util.decorators import Appender | ||
import re | ||
|
@@ -632,9 +632,10 @@ def str_split(arr, pat=None, n=None, return_type='series'): | |
pat : string, default None | ||
String or regular expression to split on. If None, splits on whitespace | ||
n : int, default None (all) | ||
return_type : {'series', 'frame'}, default 'series | ||
return_type : {'series', 'index', 'frame'}, default 'series' | ||
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. Maybe make this |
||
If frame, returns a DataFrame (elements are strings) | ||
If series, returns an Series (elements are lists of strings). | ||
If series or index, returns the same type as the original object | ||
(elements are lists of strings). | ||
|
||
Notes | ||
----- | ||
|
@@ -646,9 +647,13 @@ def str_split(arr, pat=None, n=None, return_type='series'): | |
""" | ||
from pandas.core.series import Series | ||
from pandas.core.frame import DataFrame | ||
from pandas.core.index import Index | ||
|
||
if return_type not in ('series', 'frame'): | ||
raise ValueError("return_type must be {'series', 'frame'}") | ||
if return_type not in ('series', 'index', 'frame'): | ||
raise ValueError("return_type must be {'series', 'index', 'frame'}") | ||
if return_type == 'frame' and isinstance(arr, Index): | ||
raise ValueError("return_type='frame' is not supported for string " | ||
"methods on Index") | ||
if pat is None: | ||
if n is None or n == 0: | ||
n = -1 | ||
|
@@ -928,9 +933,9 @@ def do_copy(target): | |
class StringMethods(object): | ||
|
||
""" | ||
Vectorized string functions for Series. NAs stay NA unless handled | ||
otherwise by a particular method. Patterned after Python's string methods, | ||
with some inspiration from R's stringr package. | ||
Vectorized string functions for Series and Index. NAs stay NA unless | ||
handled otherwise by a particular method. Patterned after Python's string | ||
methods, with some inspiration from R's stringr package. | ||
|
||
Examples | ||
-------- | ||
|
@@ -959,11 +964,18 @@ def __iter__(self): | |
def _wrap_result(self, result): | ||
from pandas.core.series import Series | ||
from pandas.core.frame import DataFrame | ||
from pandas.core.index import Index | ||
|
||
if not hasattr(result, 'ndim'): | ||
return result | ||
elif result.ndim == 1: | ||
name = getattr(result, 'name', None) | ||
if isinstance(self.series, Index): | ||
# if result is a boolean np.array, return the np.array | ||
# instead of wrapping it into a boolean Index (GH 8875) | ||
if is_bool_dtype(result): | ||
return result | ||
return Index(result, name=name or self.series.name) | ||
return Series(result, index=self.series.index, | ||
name=name or self.series.name) | ||
else: | ||
|
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
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.
It would be nice to add an example or two here, both to show off the new feature and to illustrate the behavior with boolean output.
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.
good idea, will do