Skip to content

Commit

Permalink
update API for Sample.keep() and Sample.drop() (#59)
Browse files Browse the repository at this point in the history
* SAMPLE: accept single strings as arg features
* SAMPLE: raise ValueError when features passed to Sample.drop are unknown
  • Loading branch information
j-ittner authored Sep 11, 2020
1 parent 46449df commit 73dd445
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/facet/_facet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pandas as pd

from pytools.api import AllTracker, is_list_like
from pytools.api import AllTracker, is_list_like, to_list, to_set

__all__ = ["Sample"]

Expand Down Expand Up @@ -276,13 +276,16 @@ def subsample(
)
return subsample

def keep(self, features: Sequence[str]) -> "Sample":
def keep(self, features: Union[str, Collection[str]]) -> "Sample":
"""
Return a new sample which only includes the features with the given names.
:param features: names of the features to be selected
:param features: name(s) of the features to be selected
:return: copy of this sample, containing only the features with the given names
"""

features: List[str] = to_list(features, element_type=str)

if not set(features).issubset(self._features):
raise ValueError(
"arg features is not a subset of the features in this sample"
Expand All @@ -299,14 +302,19 @@ def keep(self, features: Sequence[str]) -> "Sample":

return subsample

def drop(self, features: Collection[str]) -> "Sample":
def drop(self, features: Union[str, Collection[str]]) -> "Sample":
"""
Return a copy of this sample, dropping the features with the given names.
:param features: names of the features to be dropped
:param features: name(s) of the features to be dropped
:return: copy of this sample, excluding the features with the given names
"""
features = set(features)
features: Set[str] = to_set(features, element_type=str)

unknown = features.difference(self._features)
if unknown:
raise ValueError(f"unknown features in arg features: {unknown}")

return self.keep(
features=[feature for feature in self._features if feature not in features]
)
Expand Down

0 comments on commit 73dd445

Please sign in to comment.