-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: add NDFrame.select_str #27340
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
ENH: add NDFrame.select_str #27340
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
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 |
---|---|---|
|
@@ -4640,6 +4640,89 @@ def _reindex_with_indexers( | |
|
||
return self._constructor(new_data).__finalize__(self) | ||
|
||
def select_str( | ||
self, *, startswith=None, endswith=None, regex=None, flags=0, axis=None | ||
): | ||
""" | ||
Select rows or columns of dataframe from the string labels in the selected axis. | ||
|
||
Only one of keywords arguments `startswith`, `endswith` and `regex` can be used. | ||
|
||
Parameters | ||
---------- | ||
startswith: str, optional | ||
Test if the start of each string element matches a pattern. | ||
Equivalent to :meth:`str.startswith`. | ||
endswith: str, optional | ||
Test if the end of each string element matches a pattern. | ||
Equivalent to :meth:`str.endsswith`. | ||
regex : str, optional | ||
Keep labels from axis for which re.search(regex, label) is True. | ||
flags : int, default 0 (no flags) | ||
re module flags, e.g. re.IGNORECASE. Can only be used with parameter regex. | ||
axis : int or string axis name | ||
The axis to filter on. By default this is the info axis, | ||
'index' for Series, 'columns' for DataFrame. | ||
|
||
Returns | ||
------- | ||
same type as input object | ||
|
||
See Also | ||
-------- | ||
DataFrame.loc | ||
DataFrame.select_dtypes | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame(np.array(([1, 2, 3], [4, 5, 6])), | ||
... index=['mouse', 'rabbit'], | ||
... columns=['one', 'two', 'three']) | ||
|
||
>>> df.select_str(startswith='t') | ||
two three | ||
mouse 2 3 | ||
rabbit 5 6 | ||
|
||
>>> # select columns by regular expression | ||
>>> df.select_str(regex=r'e$', axis=1) | ||
one three | ||
mouse 1 3 | ||
rabbit 4 6 | ||
|
||
>>> # select rows containing 'bbi' | ||
>>> df.select_str(regex=r'bbi', axis=0) | ||
one two three | ||
rabbit 4 5 6 | ||
""" | ||
import re | ||
|
||
num_kw = com.count_not_none(startswith, endswith, regex) | ||
if num_kw != 1: | ||
raise TypeError( | ||
"Only one of keywords arguments `startswith`, `endswith` and " | ||
"`regex` can be used." | ||
) | ||
if regex is None and flags != 0: | ||
raise ValueError("Can only be used togehter with parameter 'regex'") | ||
|
||
if axis is None: | ||
axis = self._info_axis_name | ||
labels = self._get_axis(axis) | ||
|
||
if startswith is not None: | ||
mapped = labels.str.startswith(startswith) | ||
elif endswith is not None: | ||
mapped = labels.str.endsswith(endswith) | ||
else: # regex | ||
matcher = re.compile(regex, flags=flags) | ||
|
||
def f(x): | ||
return matcher.search(x) is not None | ||
|
||
mapped = labels.map(f) | ||
return self.loc(axis=axis)[mapped] | ||
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. so I would instead implement
|
||
|
||
def filter(self, items=None, like=None, regex=None, axis=None): | ||
""" | ||
Subset rows or columns of dataframe according to labels in | ||
|
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.
Can we not just boil the signature down to
regex
?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.
Yeah, startswith and endswith are not cardinal points for me, just thought they are often used, so nicer for people who don't want/know how to use regexes.