Skip to content

Commit

Permalink
Rechunk original frame
Browse files Browse the repository at this point in the history
  • Loading branch information
mcrumiller committed Feb 16, 2024
1 parent a473c11 commit 943f91e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 7 additions & 4 deletions crates/polars-core/src/frame/row/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,13 @@ impl DataFrame {

/// Transpose a DataFrame. This is a very expensive operation.
pub fn transpose(
&self,
&mut self,
keep_names_as: Option<&str>,
new_col_names: Option<Either<String, Vec<String>>>,
) -> PolarsResult<DataFrame> {
// We must iterate columns as [`AnyValue`], so we must be contiguous.
self.as_single_chunk_par();

let mut df = Cow::Borrowed(self); // Can't use self because we might drop a name column
let names_out = match new_col_names {
None => (0..self.height()).map(|i| format!("column_{i}")).collect(),
Expand Down Expand Up @@ -263,7 +266,7 @@ mod test {

#[test]
fn test_transpose() -> PolarsResult<()> {
let df = df![
let mut df = df![
"a" => [1, 2, 3],
"b" => [10, 20, 30],
]?;
Expand All @@ -277,7 +280,7 @@ mod test {
]?;
assert!(out.equals_missing(&expected));

let df = df![
let mut df = df![
"a" => [Some(1), None, Some(3)],
"b" => [Some(10), Some(20), None],
]?;
Expand All @@ -290,7 +293,7 @@ mod test {
]?;
assert!(out.equals_missing(&expected));

let df = df![
let mut df = df![
"a" => ["a", "b", "c"],
"b" => [Some(10), Some(20), None],
]?;
Expand Down
6 changes: 5 additions & 1 deletion py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1371,7 +1371,11 @@ impl PyDataFrame {
}

#[pyo3(signature = (keep_names_as, column_names))]
pub fn transpose(&self, keep_names_as: Option<&str>, column_names: &PyAny) -> PyResult<Self> {
pub fn transpose(
&mut self,
keep_names_as: Option<&str>,
column_names: &PyAny,
) -> PyResult<Self> {
let new_col_names = if let Ok(name) = column_names.extract::<Vec<String>>() {
Some(Either::Right(name))
} else if let Ok(name) = column_names.extract::<String>() {
Expand Down

0 comments on commit 943f91e

Please sign in to comment.