Skip to content
Draft
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
6 changes: 1 addition & 5 deletions crates/polars-core/src/frame/row/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ impl DataFrame {
// wasting time transposing
polars_ensure!(names_out.iter().all(|a| a.as_str() != cn), Duplicate: "{} is already in output column names", cn)
}
polars_ensure!(
df.height() != 0 && df.width() != 0,
NoData: "unable to transpose an empty DataFrame"
);
let dtype = df.get_supertype().unwrap()?;
let dtype = df.get_supertype().unwrap_or(Ok(DataType::Null))?;
df.transpose_from_dtype(&dtype, keep_names_as.map(PlSmallStr::from_str), &names_out)
}
}
Expand Down
20 changes: 20 additions & 0 deletions py-polars/tests/unit/operations/test_transpose.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,26 @@ def test_transpose_multiple_chunks() -> None:
assert_frame_equal(df.vstack(df).transpose(), expected)


def test_transpose_empty() -> None:
assert_frame_equal(pl.DataFrame().transpose(), pl.DataFrame())
assert_frame_equal(
pl.DataFrame().transpose(include_header=True),
pl.DataFrame(schema={"column": pl.String}),
)

assert pl.DataFrame(schema={"a": pl.Int32, "b": pl.Int32}).transpose().shape == (
2,
0,
)

assert_frame_equal(
pl.DataFrame(schema={"a": pl.Int32, "b": pl.Int32}).transpose(
include_header=True
),
pl.DataFrame({"column": ["a", "b"]}),
)


def test_nested_struct_transpose_21923() -> None:
df = pl.DataFrame({"x": [{"a": {"b": 1, "c": 2}}]})
assert df.transpose().item() == df.item()
Loading