Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure series are contiguous prior to transpose #14527

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -84,10 +84,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 @@ -260,7 +263,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 @@ -274,7 +277,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 @@ -287,7 +290,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
6 changes: 6 additions & 0 deletions py-polars/tests/unit/operations/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,9 @@ def test_transpose_name_from_column_13777() -> None:
csv_file = io.BytesIO(b"id,kc\nhi,3")
df = pl.read_csv(csv_file).transpose(column_names="id")
assert_series_equal(df.to_series(0), pl.Series("hi", [3]))


def test_transpose_multiple_chunks() -> None:
df = pl.DataFrame({"a": ["1"]})
expected = pl.DataFrame({"column_0": ["1"], "column_1": ["1"]})
assert_frame_equal(df.vstack(df).transpose(), expected)
Loading