Skip to content

Commit

Permalink
fmt largelists for Series
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 6, 2020
1 parent 46dcd17 commit 3e92266
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 48 deletions.
1 change: 0 additions & 1 deletion polars/src/chunked_array/upstream_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ impl<'a> FromIterator<&'a Series> for LargeListChunked {
}
}


impl FromIterator<Option<Series>> for LargeListChunked {
fn from_iter<I: IntoIterator<Item = Option<Series>>>(iter: I) -> Self {
// we don't know the type of the series until we get Some(Series) from the iterator.
Expand Down
85 changes: 56 additions & 29 deletions polars/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ impl Debug for Series {
write![f, "Series: '{}' [{}]\n[\n", $name, $dtype]?;

for i in 0..limit {
let v = $a.get_any(i);
write!(f, "\t{}\n", v)?;
let opt_v = $a.get(i);
match opt_v {
Some(v) => write!(f, "\t{}\n", v)?,
None => write!(f, "\tnull\n")?,
}
}

write![f, "]"]
Expand Down Expand Up @@ -125,26 +128,37 @@ impl Debug for Series {
write![f, "]"]
}
Series::LargeList(a) => {
let limit = 3;
write![f, "Series: list \n[\n"]?;
a.into_iter().take(limit).for_each(|opt_s| match opt_s {
None => {
write!(f, "\tnull\n").ok();
}
Some(s) => {
// Inner series are indented one level
let series_formatted = format!("{:?}", s);

// now prepend a tab before every line.
let mut with_tab = String::with_capacity(series_formatted.len() + 10);
for line in series_formatted.split("\n") {
with_tab.push_str(&format!("\t{}\n", line))
}
write![f, "Series: '{}' [{}]\n[\n", self.name(), "list"]?;

write!(f, "\t{}\n", with_tab).ok();
for i in 0..limit {
let opt_v = a.get(i);
match opt_v {
Some(v) => write!(f, "\t{}\n", v.fmt_largelist())?,
None => write!(f, "\tnull\n")?,
}
});
}

write![f, "]"]
// let limit = 3;
// write![f, "Series: list \n[\n"]?;
// a.into_iter().take(limit).for_each(|opt_s| match opt_s {
// None => {
// write!(f, "\tnull\n").ok();
// }
// Some(s) => {
// // Inner series are indented one level
// let series_formatted = format!("{:?}", s);
//
// // now prepend a tab before every line.
// let mut with_tab = String::with_capacity(series_formatted.len() + 10);
// for line in series_formatted.split("\n") {
// with_tab.push_str(&format!("\t{}\n", line))
// }
//
// write!(f, "\t{}\n", with_tab).ok();
// }
// });
// write![f, "]"]
}
}
}
Expand Down Expand Up @@ -280,22 +294,35 @@ impl Display for AnyType<'_> {
}
}

macro_rules! fmt_option {
($opt:expr) => {{
match $opt {
Some(v) => format!("{:?}", v),
None => "null".to_string(),
}
}};
}

macro_rules! impl_fmt_largelist {
($self:ident) => {{
match $self.len() {
1 => format!("[{:?}]", $self.get(0)),
2 => format!("[{:?}, {:?}]", $self.get(0), $self.get(1)),
1 => format!("[{}]", fmt_option!($self.get(0))),
2 => format!(
"[{}, {}]",
fmt_option!($self.get(0)),
fmt_option!($self.get(1))
),
3 => format!(
"[{:?}, {:?}, {:?}]",
$self.get(0),
$self.get(1),
$self.get(2)
"[{}, {}, {}]",
fmt_option!($self.get(0)),
fmt_option!($self.get(1)),
fmt_option!($self.get(2))
),
_ => format!(
"[{:?}, {:?}, ... {:?}]",
$self.get(0),
$self.get(1),
$self.get($self.len() - 1)
"[{}, {}, ... {}]",
fmt_option!($self.get(0)),
fmt_option!($self.get(1)),
fmt_option!($self.get($self.len() - 1))
),
}
}};
Expand Down
56 changes: 38 additions & 18 deletions polars/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,35 @@ impl DataFrame {
/// let s1 = Series::new("temp", [22.1, 19.9, 7.].as_ref());
/// let df = DataFrame::new(vec![s0, s1]).unwrap();
/// ```
pub fn new(columns: Vec<Series>) -> Result<Self> {
if !columns.iter().map(|s| s.len()).all_equal() {
return Err(PolarsError::ShapeMisMatch);
pub fn new<S: IntoSeries>(columns: Vec<S>) -> Result<Self> {
let mut first_len = None;
let mut series_cols = Vec::with_capacity(columns.len());

// check for series length equality and convert into series in one pass
for s in columns {
let series = s.into_series();
match first_len {
Some(len) => {
if series.len() != len {
return Err(PolarsError::ShapeMisMatch);
}
}
None => first_len = Some(series.len()),
}
series_cols.push(series)
}
let mut df = DataFrame { columns };
let mut df = DataFrame {
columns: series_cols,
};
df.rechunk()?;
Ok(df)
}

// doesn't check Series sizes.
fn new_no_checks(columns: Vec<Series>) -> DataFrame {
DataFrame { columns }
}

/// Ensure all the chunks in the DataFrame are aligned.
fn rechunk(&mut self) -> Result<&mut Self> {
let chunk_lens = self
Expand Down Expand Up @@ -276,7 +296,7 @@ impl DataFrame {
}
});

DataFrame::new(new_cols)
Ok(DataFrame::new_no_checks(new_cols))
}

/// Get a row in the dataframe. Beware this is slow.
Expand Down Expand Up @@ -351,7 +371,7 @@ impl DataFrame {
S: Selection<'a>,
{
let selected = self.select_series(selection)?;
let df = DataFrame::new(selected)?;
let df = DataFrame::new_no_checks(selected);
Ok(df)
}

Expand Down Expand Up @@ -387,7 +407,7 @@ impl DataFrame {
.par_iter()
.map(|col| col.filter(mask))
.collect::<Result<Vec<_>>>()?;
DataFrame::new(new_col)
Ok(DataFrame::new_no_checks(new_col))
}

/// Take DataFrame value by indexes from an iterator.
Expand All @@ -413,7 +433,7 @@ impl DataFrame {
s.take_iter(&mut i, capacity)
})
.collect::<Result<Vec<_>>>()?;
DataFrame::new(new_col)
Ok(DataFrame::new_no_checks(new_col))
}

/// Take DataFrame values by indexes from an iterator. This doesn't do any bound checking.
Expand All @@ -439,7 +459,7 @@ impl DataFrame {
s.take_iter_unchecked(&mut i, capacity)
})
.collect::<Vec<_>>();
DataFrame::new(new_col).unwrap()
DataFrame::new_no_checks(new_col)
}

/// Take DataFrame values by indexes from an iterator that may contain None values.
Expand All @@ -465,7 +485,7 @@ impl DataFrame {
s.take_opt_iter(&mut i, capacity)
})
.collect::<Result<Vec<_>>>()?;
DataFrame::new(new_col)
Ok(DataFrame::new_no_checks(new_col))
}

/// Take DataFrame values by indexes from an iterator that may contain None values.
Expand All @@ -492,7 +512,7 @@ impl DataFrame {
s.take_opt_iter_unchecked(&mut i, capacity)
})
.collect::<Vec<_>>();
DataFrame::new(new_col).unwrap()
DataFrame::new_no_checks(new_col)
}

/// Take DataFrame rows by index values.
Expand All @@ -513,7 +533,7 @@ impl DataFrame {
.map(|s| s.take(indices))
.collect::<Result<Vec<_>>>()?;

DataFrame::new(new_col)
Ok(DataFrame::new_no_checks(new_col))
}

/// Rename a column in the DataFrame
Expand Down Expand Up @@ -791,7 +811,7 @@ impl DataFrame {
.par_iter()
.map(|s| s.slice(offset, length))
.collect::<Result<Vec<_>>>()?;
DataFrame::new(col)
Ok(DataFrame::new_no_checks(col))
}

/// Get the head of the DataFrame
Expand All @@ -801,7 +821,7 @@ impl DataFrame {
.iter()
.map(|s| s.head(length))
.collect::<Vec<_>>();
DataFrame::new(col).unwrap()
DataFrame::new_no_checks(col)
}

/// Get the tail of the DataFrame
Expand All @@ -811,7 +831,7 @@ impl DataFrame {
.iter()
.map(|s| s.tail(length))
.collect::<Vec<_>>();
DataFrame::new(col).unwrap()
DataFrame::new_no_checks(col)
}

/// Transform the underlying chunks in the DataFrame to Arrow RecordBatches
Expand Down Expand Up @@ -862,7 +882,7 @@ impl DataFrame {
/// Get a DataFrame with all the columns in reversed order
pub fn reverse(&self) -> Self {
let col = self.columns.iter().map(|s| s.reverse()).collect::<Vec<_>>();
DataFrame::new(col).unwrap()
DataFrame::new_no_checks(col)
}

/// Shift the values by a given period and fill the parts that will be empty due to this operation
Expand All @@ -875,7 +895,7 @@ impl DataFrame {
.iter()
.map(|s| s.shift(periods))
.collect::<Result<Vec<_>>>()?;
DataFrame::new(col)
Ok(DataFrame::new_no_checks(col))
}

/// Replace None values with one of the following strategies:
Expand All @@ -892,7 +912,7 @@ impl DataFrame {
.iter()
.map(|s| s.fill_none(strategy))
.collect::<Result<Vec<_>>>()?;
DataFrame::new(col)
Ok(DataFrame::new_no_checks(col))
}

pub fn pipe<F, B>(self, f: F) -> Result<B>
Expand Down

0 comments on commit 3e92266

Please sign in to comment.