Skip to content

Commit

Permalink
Add support for Selector arg to SelectorMethods.select().
Browse files Browse the repository at this point in the history
  • Loading branch information
lebedov committed Mar 22, 2016
1 parent dbf2de9 commit 0e4dd7f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions neurokernel/plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1867,9 +1867,9 @@ def select(cls, df, selector, start=None, stop=None):
----------
df : pandas.DataFrame
DataFrame instance on which to apply the selector.
selector : str, unicode, or sequence
Selector string (e.g., '/foo[0:2]') or sequence of token sequences
(e.g., [['foo', (0, 2)]]).
selector : Selector, str, unicode, or sequence
Selector class instance, string (e.g., '/foo[0:2]') or
sequence of token sequences (e.g., [['foo', (0, 2)]]).
start, stop : int
Start and end indices in `row` over which to test entries.
Expand All @@ -1880,7 +1880,15 @@ def select(cls, df, selector, start=None, stop=None):
"""

assert cls.is_selector(selector)
if type(selector) in [str, unicode]:
if isinstance(selector, Selector):
if len(df.index.names[start:stop])>1:
try:
tks = list(selector.expanded)
return df[tks]
except:
pass
parse_list = list(selector.expanded)
elif type(selector) in [str, unicode]:
if len(df.index.names[start:stop])>1:
try:
tks = cls.expand(selector)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_plsel.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ def test_select_Selector(self):

def test_select_comma(self):
result = self.sel.select(self.df, '/foo/qux,/baz/mof')
idx = pd.MultiIndex.from_tuples([('foo','qux',0),
('foo','qux',1),
('baz','mof',0)], names=[0, 1, 2])
idx = pd.MultiIndex.from_tuples([('foo','qux', 0),
('foo','qux', 1),
('baz','mof', 0)], names=[0, 1, 2])
assert_frame_equal(result, self.df.ix[idx])

def test_select_plus(self):
Expand Down

0 comments on commit 0e4dd7f

Please sign in to comment.