Skip to content

Commit

Permalink
fix[python]: keep column type of empty arrow table (#4759)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 7, 2022
1 parent e46ce2d commit 56d2e1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,12 @@ def arrow_to_pydf(

# path for table without rows that keeps datatype
if tbl.shape[0] == 0:
pydf = pli.DataFrame._from_pandas(tbl.to_pandas())._df
pydf = pli.DataFrame(
[
pli.Series(name, c)
for (name, c) in zip(tbl.column_names, tbl.columns)
]
)._df
else:
pydf = PyDataFrame.from_arrow_record_batches(tbl.to_batches())
else:
Expand Down
8 changes: 7 additions & 1 deletion py-polars/tests/unit/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,18 @@ def test_from_empty_arrow() -> None:
tbl = pa.Table.from_pandas(df1)
out = pl.from_arrow(tbl)
assert out.columns == ["b", "__index_level_0__"]
assert out.dtypes == [pl.Float64, pl.Utf8]
assert out.dtypes == [pl.Float64, pl.Int8]
tbl = pa.Table.from_pandas(df1, preserve_index=False)
out = pl.from_arrow(tbl)
assert out.columns == ["b"]
assert out.dtypes == [pl.Float64]

# 4568
tbl = pa.table({"l": []}, schema=pa.schema([("l", pa.large_list(pa.uint8()))]))

df = pl.from_arrow(tbl)
assert df.schema["l"] == pl.List(pl.UInt8)


def test_from_null_column() -> None:
assert pl.from_pandas(pd.DataFrame(data=[pd.NA, pd.NA])).shape == (2, 1)
Expand Down

0 comments on commit 56d2e1d

Please sign in to comment.