Skip to content

Commit

Permalink
Merge pull request #3737 from DanteOz/colsplitter-on-value
Browse files Browse the repository at this point in the history
Add split value argument to ColSplitter
  • Loading branch information
jph00 committed Jul 17, 2022
2 parents fa188e5 + 8ae46d8 commit caf805e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
7 changes: 5 additions & 2 deletions fastai/data/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@ def _inner(o): return FuncSplitter(_func)(o)
return _inner

# Cell
def ColSplitter(col='is_valid'):
def ColSplitter(col='is_valid', on=None):
"Split `items` (supposed to be a dataframe) by value in `col`"
def _inner(o):
assert isinstance(o, pd.DataFrame), "ColSplitter only works when your items are a pandas DataFrame"
valid_idx = (o.iloc[:,col] if isinstance(col, int) else o[col]).values.astype('bool')
c = o.iloc[:,col] if isinstance(col, int) else o[col]
if on is None: valid_idx = c.values.astype('bool')
elif is_listy(on): valid_idx = c.isin(on)
else: valid_idx = c == on
return IndexSplitter(mask2idxs(valid_idx))(o)
return _inner

Expand Down
Loading

0 comments on commit caf805e

Please sign in to comment.