Skip to content

Commit

Permalink
return as much DataFrames, Series and ChunkedArray references as poss…
Browse files Browse the repository at this point in the history
…ible to make piping easy
  • Loading branch information
ritchie46 committed Aug 28, 2020
1 parent a6231fb commit 9c01c37
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
2 changes: 1 addition & 1 deletion polars/src/frame/hash_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl DataFrame {
}

for name in rename_strs {
df_right.rename(&name, &format!("{}_right", name))?
df_right.rename(&name, &format!("{}_right", name))?;
}

df_left.hstack(&df_right.columns)?;
Expand Down
46 changes: 19 additions & 27 deletions polars/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl DataFrame {
}

/// Ensure all the chunks in the DataFrame are aligned.
fn rechunk(&mut self) -> Result<()> {
fn rechunk(&mut self) -> Result<&mut Self> {
let chunk_lens = self
.columns
.iter()
Expand All @@ -97,7 +97,7 @@ impl DataFrame {
let new_col = col.rechunk(Some(&chunk_id))?;
self.columns[idx] = new_col;
}
Ok(())
Ok(self)
}

/// Get a reference to the DataFrame schema.
Expand Down Expand Up @@ -200,7 +200,7 @@ impl DataFrame {
/// df.hstack(columns);
/// }
/// ```
pub fn hstack(&mut self, columns: &[DfSeries]) -> Result<()> {
pub fn hstack(&mut self, columns: &[DfSeries]) -> Result<&mut Self> {
let height = self.height();
for col in columns {
if col.len() != height {
Expand All @@ -210,7 +210,7 @@ impl DataFrame {
}
}
self.register_mutation()?;
Ok(())
Ok(self)
}

/// Remove column by name
Expand All @@ -233,7 +233,7 @@ impl DataFrame {
/// Drop a column by name.
/// This is a pure method and will return a new DataFrame instead of modifying
/// the current one in place.
pub fn drop(&self, name: &str) -> Result<DataFrame> {
pub fn drop(&self, name: &str) -> Result<Self> {
let idx = self.name_to_idx(name)?;
let mut new_cols = Vec::with_capacity(self.columns.len() - 1);

Expand Down Expand Up @@ -318,7 +318,7 @@ impl DataFrame {
/// }
/// }
/// ```
pub fn select<'a, S>(&self, selection: S) -> Result<DataFrame>
pub fn select<'a, S>(&self, selection: S) -> Result<Self>
where
S: Selection<'a>,
{
Expand Down Expand Up @@ -489,20 +489,21 @@ impl DataFrame {
///
/// ```
/// use polars::prelude::*;
/// fn example(df: &mut DataFrame) -> Result<()> {
/// fn example(df: &mut DataFrame) -> Result<&mut DataFrame> {
/// let original_name = "foo";
/// let new_name = "bar";
/// df.rename(original_name, new_name)
/// }
/// ```
pub fn rename(&mut self, column: &str, name: &str) -> Result<()> {
pub fn rename(&mut self, column: &str, name: &str) -> Result<&mut Self> {
self.select_mut(column)
.ok_or(PolarsError::NotFound)
.map(|s| s.rename(name))
.map(|s| s.rename(name))?;
Ok(self)
}

/// Sort DataFrame in place by a column.
pub fn sort_in_place(&mut self, by_column: &str, reverse: bool) -> Result<()> {
pub fn sort_in_place(&mut self, by_column: &str, reverse: bool) -> Result<&mut Self> {
let s = match self.column(by_column) {
Some(s) => s,
None => return Err(PolarsError::NotFound),
Expand All @@ -515,7 +516,7 @@ impl DataFrame {
.par_iter()
.map(|s| s.take(&take))
.collect::<Result<Vec<_>>>()?;
Ok(())
Ok(self)
}

/// Return a sorted clone of this DataFrame.
Expand All @@ -530,9 +531,8 @@ impl DataFrame {
}

/// Replace a column with a series.
pub fn replace(&mut self, column: &str, new_col: DfSeries) -> Result<()> {
let idx = self.find_idx_by_name(column).ok_or(PolarsError::NotFound)?;
self.replace_at_idx(idx, new_col)
pub fn replace(&mut self, column: &str, new_col: DfSeries) -> Result<&mut Self> {
self.apply(column, |_| new_col)
}

/// Replace column at index `idx` with a series.
Expand All @@ -548,16 +548,8 @@ impl DataFrame {
/// // Add 32 to get lowercase ascii values
/// df.replace_at_idx(1, df.select_at_idx(1).unwrap() + 32);
/// ```
pub fn replace_at_idx(&mut self, idx: usize, new_col: DfSeries) -> Result<()> {
let opt_col = self.columns.get_mut(idx);
match opt_col {
Some(col) => {
let _ = mem::replace(col, new_col);
self.register_mutation()?;
Ok(())
}
_ => Err(PolarsError::OutOfBounds),
}
pub fn replace_at_idx(&mut self, idx: usize, new_col: DfSeries) -> Result<&mut Self> {
self.apply_at_idx(idx, |_| new_col)
}

/// Apply a closure to a column. This is the recommended way to do in place modification.
Expand Down Expand Up @@ -599,7 +591,7 @@ impl DataFrame {
/// | "egg" | 3 |
/// +--------+-----+
/// ```
pub fn apply<F>(&mut self, column: &str, f: F) -> Result<()>
pub fn apply<F>(&mut self, column: &str, f: F) -> Result<&mut Self>
where
F: FnOnce(&Series) -> Series,
{
Expand Down Expand Up @@ -636,14 +628,14 @@ impl DataFrame {
/// | "egg" | 111 |
/// +--------+-------+
/// ```
pub fn apply_at_idx<F>(&mut self, idx: usize, f: F) -> Result<()>
pub fn apply_at_idx<F>(&mut self, idx: usize, f: F) -> Result<&mut Self>
where
F: FnOnce(&Series) -> Series,
{
let col = self.columns.get_mut(idx).ok_or(PolarsError::OutOfBounds)?;
let _ = mem::replace(col, f(col));
self.register_mutation()?;
Ok(())
Ok(self)
}

/// Slice the DataFrame along the rows.
Expand Down
17 changes: 10 additions & 7 deletions polars/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,9 @@ impl Series {
}

/// Rename series.
pub fn rename(&mut self, name: &str) {
apply_method_all_series!(self, rename, name)
pub fn rename(&mut self, name: &str) -> &mut Self {
apply_method_all_series!(self, rename, name);
self
}

/// Get field (used in schema)
Expand Down Expand Up @@ -490,8 +491,9 @@ impl Series {
unpack_series!(self, IntervalYearMonth)
}

pub fn append_array(&mut self, other: ArrayRef) -> Result<()> {
apply_method_all_series!(self, append_array, other)
pub fn append_array(&mut self, other: ArrayRef) -> Result<&mut Self> {
apply_method_all_series!(self, append_array, other)?;
Ok(self)
}

/// Take `num_elements` from the top as a zero copy view.
Expand All @@ -505,10 +507,10 @@ impl Series {
}

/// Append a Series of the same type in place.
pub fn append(&mut self, other: &Self) -> Result<()> {
pub fn append(&mut self, other: &Self) -> Result<&mut Self> {
if self.dtype() == other.dtype() {
apply_method_all_series!(self, append, other.as_ref());
Ok(())
Ok(self)
} else {
Err(PolarsError::DataTypeMisMatch)
}
Expand Down Expand Up @@ -628,8 +630,9 @@ impl Series {
}

/// Sort in place.
pub fn sort_in_place(&mut self, reverse: bool) {
pub fn sort_in_place(&mut self, reverse: bool) -> &mut Self {
apply_method_all_series!(self, sort_in_place, reverse);
self
}

pub fn sort(&self, reverse: bool) -> Self {
Expand Down

0 comments on commit 9c01c37

Please sign in to comment.