Skip to content

Commit

Permalink
Selection trait accept String slice
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 13, 2020
1 parent 1d19790 commit 0ada2be
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions polars/src/frame/group_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl DataFrame {
/// .sum()
/// }
/// ```
pub fn groupby<'g, S: Selection<'g>>(&self, by: S) -> Result<GroupBy> {
pub fn groupby<'g, J, S: Selection<'g, J>>(&self, by: S) -> Result<GroupBy> {
let selected_keys = self.select_series(by)?;

let groups = match selected_keys.len() {
Expand Down Expand Up @@ -457,9 +457,9 @@ impl AggFirst for LargeListChunked {}
impl<'df, 'selection_str> GroupBy<'df, 'selection_str> {
/// Select the column by which the determine the groups.
/// You can select a single column or a slice of columns.
pub fn select<S>(mut self, selection: S) -> Self
pub fn select<S, J>(mut self, selection: S) -> Self
where
S: Selection<'selection_str>,
S: Selection<'selection_str, J>,
{
self.selected_agg = Some(selection.to_selection_vec());
self
Expand Down
8 changes: 4 additions & 4 deletions polars/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,19 @@ impl DataFrame {
/// }
/// }
/// ```
pub fn select<'a, S>(&self, selection: S) -> Result<Self>
pub fn select<'a, S, J>(&self, selection: S) -> Result<Self>
where
S: Selection<'a>,
S: Selection<'a, J>,
{
let selected = self.select_series(selection)?;
let df = DataFrame::new_no_checks(selected);
Ok(df)
}

/// Select column(s) from this DataFrame and return them into a Vector.
pub fn select_series<'a, S>(&self, selection: S) -> Result<Vec<Series>>
pub fn select_series<'a, S, J>(&self, selection: S) -> Result<Vec<Series>>
where
S: Selection<'a>,
S: Selection<'a, J>,
{
let cols = selection.to_selection_vec();
let selected = cols
Expand Down
23 changes: 12 additions & 11 deletions polars/src/frame/select.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
pub trait Selection<'a> {
pub trait Selection<'a, S> {
fn to_selection_vec(self) -> Vec<&'a str>;
}

impl<'a> Selection<'a> for &'a str {
impl<'a> Selection<'a, &str> for &'a str {
fn to_selection_vec(self) -> Vec<&'a str> {
vec![self]
}
}

impl<'a> Selection<'a> for Vec<&'a str> {
impl<'a> Selection<'a, &str> for Vec<&'a str> {
fn to_selection_vec(self) -> Vec<&'a str> {
self
}
}

impl<'a, T> Selection<'a> for &T
impl<'a, T, S: 'a> Selection<'a, S> for &'a T
where
T: AsRef<[&'a str]>,
T: AsRef<[S]>,
S: AsRef<str>,
{
fn to_selection_vec(self) -> Vec<&'a str> {
self.as_ref().iter().copied().collect()
self.as_ref().iter().map(|s| s.as_ref()).collect()
}
}

impl<'a> Selection<'a> for (&'a str, &'a str) {
impl<'a> Selection<'a, &str> for (&'a str, &'a str) {
fn to_selection_vec(self) -> Vec<&'a str> {
vec![self.0, self.1]
}
}
impl<'a> Selection<'a> for (&'a str, &'a str, &'a str) {
impl<'a> Selection<'a, &str> for (&'a str, &'a str, &'a str) {
fn to_selection_vec(self) -> Vec<&'a str> {
vec![self.0, self.1, self.2]
}
}

impl<'a> Selection<'a> for (&'a str, &'a str, &'a str, &'a str) {
impl<'a> Selection<'a, &str> for (&'a str, &'a str, &'a str, &'a str) {
fn to_selection_vec(self) -> Vec<&'a str> {
vec![self.0, self.1, self.2, self.3]
}
}

impl<'a> Selection<'a> for (&'a str, &'a str, &'a str, &'a str, &'a str) {
impl<'a> Selection<'a, &str> for (&'a str, &'a str, &'a str, &'a str, &'a str) {
fn to_selection_vec(self) -> Vec<&'a str> {
vec![self.0, self.1, self.2, self.3, self.4]
}
}

impl<'a> Selection<'a> for (&'a str, &'a str, &'a str, &'a str, &'a str, &'a str) {
impl<'a> Selection<'a, &str> for (&'a str, &'a str, &'a str, &'a str, &'a str, &'a str) {
fn to_selection_vec(self) -> Vec<&'a str> {
vec![self.0, self.1, self.2, self.3, self.4, self.5]
}
Expand Down

0 comments on commit 0ada2be

Please sign in to comment.