Skip to content

Commit

Permalink
Adding support for struct columns in DataFrame.to_pandas (#3178)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjermain committed Apr 19, 2022
1 parent d7c9b1f commit 6220dcd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions polars/polars-core/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,7 @@ impl DataFrame {
.ok_or_else(|| {
PolarsError::NoData("Can not determine number of chunks if there is no data".into())
})?
.chunks()
.len())
.n_chunks())
}

/// Get a reference to the schema fields of the `DataFrame`.
Expand Down
5 changes: 5 additions & 0 deletions polars/polars-core/src/series/implementations/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl SeriesTrait for SeriesWrap<StructChunked> {
self.0.name()
}

fn chunk_lengths(&self) -> ChunkIdIter {
let s = self.0.fields().first().unwrap();
s.chunk_lengths()
}

/// Number of chunks in this Series
fn n_chunks(&self) -> usize {
let s = self.0.fields().first().unwrap();
Expand Down
11 changes: 11 additions & 0 deletions py-polars/tests/test_struct.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pandas as pd

import polars as pl


Expand Down Expand Up @@ -141,3 +143,12 @@ def test_nested_struct() -> None:
def test_eager_struct() -> None:
s = pl.struct([pl.Series([1, 2, 3]), pl.Series(["a", "b", "c"])], eager=True)
assert s.dtype == pl.Struct


def test_struct_to_pandas() -> None:
df = pd.DataFrame([{"a": {"b": {"c": 2}}}])
pl_df = pl.from_pandas(df)

assert isinstance(pl_df.dtypes[0], pl.datatypes.Struct)

assert pl_df.to_pandas().equals(df)

0 comments on commit 6220dcd

Please sign in to comment.